From 8d8b110f3b0cbb227218b0f5adb3a100b33ab794 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Tue, 2 Jan 2018 08:42:50 +0000 Subject: [PATCH] Travis build: 837 [ci skip] --- README.md | 29 +++++++++++++++++++++++++++++ docs/index.html | 10 +++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 66b72362a..78735dc9a 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ * [`chainAsync`](#chainasync) * [`compose`](#compose) * [`curry`](#curry) +* [`defer`](#defer) * [`functionName`](#functionname) * [`memoize`](#memoize) * [`runPromisesInSeries`](#runpromisesinseries) @@ -2378,6 +2379,34 @@ curry(Math.min, 3)(10)(50)(2); // 2
[⬆ Back to top](#table-of-contents) +### defer + +Defers invoking a function until the current call stack has cleared. + +Use `setTimeout()` with a timeout of 1ms to add a new event to the browser event queue and allow the rendering engine to complete its work. Use the spread (`...`) operator to supply the function with an arbitrary number of arguments. + +```js +const defer = (fn, ...args) => setTimeout(fn, 1, ...args); +``` + +
+Examples + +```js +// Example A: +defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a' + +// Example B: +document.querySelector('#someElement').innerHTML = 'Hello'; +longRunningFunction(); // the browser will not update the HTML until this has finished +defer(longRunningFunction); // the browser will update the HTML then run the function +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### functionName Logs the name of a function. diff --git a/docs/index.html b/docs/index.html index 2bab29808..8f7cd32cc 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 ]
@@ -527,6 +527,14 @@ multiplyAndAdd5(5, 2); // 15
   arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
 
curry(Math.pow)(2)(10); // 1024
 curry(Math.min, 3)(10)(50)(2); // 2
+

defer

Defers invoking a function until the current call stack has cleared.

Use setTimeout() with a timeout of 1ms to add a new event to the browser event queue and allow the rendering engine to complete its work. Use the spread (...) operator to supply the function with an arbitrary number of arguments.

const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
+
// Example A:
+defer(console.log, 'a'), console.log('b'); // logs 'b' then 'a'
+
+// Example B:
+document.querySelector('#someElement').innerHTML = 'Hello';
+longRunningFunction(); // the browser will not update the HTML until this has finished
+defer(longRunningFunction); // the browser will update the HTML then run the function
 

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.

Use Object.create(null) to create an empty object without Object.prototype (so that those properties are not resolved if the input value is something like 'hasOwnProperty'). 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.

const memoize = fn => {