diff --git a/README.md b/README.md index 78735dc9a..0c5ef52ad 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ * [`defer`](#defer) * [`functionName`](#functionname) * [`memoize`](#memoize) +* [`once`](#once) * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep) @@ -2458,6 +2459,34 @@ anagramsCached('javascript'); // returns virtually instantly since it's now cach
[⬆ Back to top](#table-of-contents) +### once + +Ensures a function is called only once. + +Utilizing a closure, use a flag, `called`, and set it to `true` once the function is called for the first time, preventing it from being called again. +Allow the function to be supplied with an arbitrary number of arguments using the spread (`...`) operator. + +```js +const once = fn => + (called => (...args) => (!called ? ((called = true), fn(...args)) : undefined))(); +``` + +
+Examples + +```js +const startApp = event => { + // initializes the app + console.log(event); // access to any arguments supplied +}; +document.addEventListener('click', once(startApp)); // only runs `startApp` once upon click +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### runPromisesInSeries Runs an array of promises in series. diff --git a/docs/index.html b/docs/index.html index 8f7cd32cc..840b6d38d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -545,6 +545,13 @@ defer(longRunningFunction); // the browser will update the HTML then run the fun
 const anagramsCached = memoize(anagrams);
 anagramsCached('javascript'); // takes a long time
 anagramsCached('javascript'); // returns virtually instantly since it's now cached
+

once

Ensures a function is called only once.

Utilizing a closure, use a flag, called, and set it to true once the function is called for the first time, preventing it from being called again. Allow the function to be supplied with an arbitrary number of arguments using the spread (...) operator.

const once = fn =>
+  (called => (...args) => (!called ? ((called = true), fn(...args)) : undefined))();
+
const startApp = event => {
+  // initializes the app
+  console.log(event); // access to any arguments supplied
+};
+document.addEventListener('click', once(startApp)); // only runs `startApp` once upon click
 

runPromisesInSeries

Runs an array of promises in series.

Use Array.reduce() to create a promise chain, where each promise returns the next promise when resolved.

const runPromisesInSeries = ps => ps.reduce((p, next) => p.then(next), Promise.resolve());
 
const delay = d => new Promise(r => setTimeout(r, d));
 runPromisesInSeries([() => delay(1000), () => delay(2000)]); // //executes each promise sequentially, taking a total of 3 seconds to complete
diff --git a/snippets/once.md b/snippets/once.md
index 18e5fc886..ea32a6843 100644
--- a/snippets/once.md
+++ b/snippets/once.md
@@ -6,7 +6,8 @@ Utilizing a closure, use a flag, `called`, and set it to `true` once the functio
 Allow the function to be supplied with an arbitrary number of arguments using the spread (`...`) operator.
 
 ```js
-const once = fn => (called => (...args) => !called ? (called = true, fn(...args)) : undefined)()
+const once = fn =>
+  (called => (...args) => (!called ? ((called = true), fn(...args)) : undefined))();
 ```
 
 ```js