diff --git a/README.md b/README.md index 8adf0c58b..8baa1bfdc 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,7 @@ average(1, 2, 3); * [`composeRight`](#composeright) * [`curry`](#curry) * [`defer`](#defer) +* [`delay`](#delay) * [`functionName`](#functionname) * [`memoize`](#memoize) * [`negate`](#negate) @@ -3360,6 +3361,35 @@ defer(longRunningFunction); // Browser will update the HTML then run the functio
[⬆ Back to top](#table-of-contents) +### defer + +Invokes the provided function after `wait` milliseconds. + +Use `setTimeout()` to delay execution of `fn`. +Use the spread (`...`) operator to supply the function with an arbitrary number of arguments. + +```js +const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args); +``` + +
+Examples + +```js +delay( + function(text) { + console.log(text); + }, + 1000, + 'later' +); // Logs 'later' after one second. +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### functionName Logs the name of a function. diff --git a/docs/index.html b/docs/index.html index 9daf0db4e..609f08592 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

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);
@@ -749,6 +749,14 @@ console.log<
 document.querySelector('#someElement').innerHTML = 'Hello';
 longRunningFunction(); //Browser will not update the HTML until this has finished
 defer(longRunningFunction); // Browser will update the HTML then run the function
+

defer

Invokes the provided function after wait milliseconds.

Use setTimeout() to delay execution of fn. Use the spread (...) operator to supply the function with an arbitrary number of arguments.

const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
+
delay(
+  function(text) {
+    console.log(text);
+  },
+  1000,
+  'later'
+); // Logs 'later' after one second.
 

functionName

Logs the name of a function.

Use console.debug() and the name property of the passed method to log the method's name to the debug channel of the console.

const functionName = fn => (console.debug(fn.name), fn);
 
functionName(Math.max); // max (logged in debug channel of console)
 

memoize

Returns the memoized (cached) function.

Create an empty cache by instantiating a new Map object. Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not. The function keyword must be used in order to allow the memoized function to have its this context changed if necessary. Allow access to the cache by setting it as a property on the returned function.

const memoize = fn => {
diff --git a/snippets/delay.md b/snippets/delay.md
index 3735fa065..09f7c2606 100644
--- a/snippets/delay.md
+++ b/snippets/delay.md
@@ -10,7 +10,11 @@ const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
 ```
 
 ```js
-delay(function(text) {
-  console.log(text);
-}, 1000, 'later'); // Logs 'later' after one second.
+delay(
+  function(text) {
+    console.log(text);
+  },
+  1000,
+  'later'
+); // Logs 'later' after one second.
 ```