From 00c14d69d58700194945a860d3829018b2d5d3b9 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Sun, 28 Jan 2018 12:31:14 +0000 Subject: [PATCH] Travis build: 1463 --- README.md | 33 +++++++++++++++++++++++++++++++++ docs/index.html | 12 +++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a577f558..0d12c1943 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ average(1, 2, 3); * [`collectInto`](#collectinto) * [`flip`](#flip) * [`over`](#over) +* [`pipeAsyncFunctions`](#pipeasyncfunctions) * [`pipeFunctions`](#pipefunctions) * [`promisify`](#promisify) * [`spreadOver`](#spreadover) @@ -554,6 +555,38 @@ minMax(1, 2, 3, 4, 5); // [1,5]
[⬆ Back to top](#table-of-contents) +### pipeAsyncFunctions + +Performs left-to-right function composition for asynchronous functions. + +Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition using `Promise.then()`. +The functions can return a combination of: simple values, `Promise`'s, or they can be defined as `async` ones returning through `await`. +All functions must be unary. + +```js +const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg)); +``` + +
+Examples + +```js +const sum = pipeAsyncFunctions( + x => x + 1, + x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)), + x => x + 3, + async x => (await x) + 4 +); +(async () => { + console.log(await sum(5)); // 15 (after one second) +})(); +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### pipeFunctions Performs left-to-right function composition. diff --git a/docs/index.html b/docs/index.html index 75bfadaa9..772a0519d 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);
@@ -78,6 +78,16 @@ Object.assig
 

over

Creates a function that invokes each provided function with the arguments it receives and returns the results.

Use Array.map() and Function.apply() to apply each function to the given arguments.

const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
 
const minMax = over(Math.min, Math.max);
 minMax(1, 2, 3, 4, 5); // [1,5]
+

pipeAsyncFunctions

Performs left-to-right function composition for asynchronous functions.

Use Array.reduce() with the spread operator (...) to perform left-to-right function composition using Promise.then(). The functions can return a combination of: simple values, Promise's, or they can be defined as async ones returning through await. All functions must be unary.

const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
+
const sum = pipeAsyncFunctions(
+  x => x + 1,
+  x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
+  x => x + 3,
+  async x => (await x) + 4
+);
+(async () => {
+  console.log(await sum(5)); // 15 (after one second)
+})();
 

pipeFunctions

Performs left-to-right function composition.

Use Array.reduce() with the spread operator (...) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
 
const add5 = x => x + 5;
 const multiply = (x, y) => x * y;