-
Notifications
You must be signed in to change notification settings - Fork 0
Library: seq()
Eugene Lazutkin edited this page Jul 5, 2018
·
1 revision
seq() module is a function, which creates a chain of asynchronous functions specified dynamically as an array. Each function will be called with a resolved result of a previous function.
For example, for three functions f, g, h, which return a Promise object, all those examples are identical:
seq(f, g, h)(init).then(result => console.log(result));
seq([f, g, h])(init).then(result => console.log(result));
(async init => {
const x = await f(init);
const y = await g(x);
const z = await h(y);
return z;
})(init).then(result => console.log(result));
Promise.resolve(init)
.then(f)
.then(g)
.then(h)
.then(result => console.log(result));In a module-enabled environment (like Babel) it can be accessed like that:
import seq from '@researchnow/reno/src/utils/seq';In global-based environments (like a browser) it is frequently mapped to Reno.utils.seq.
This function has two signatures. It accepts the following arguments:
-
fnsis an array of functions. Each function takes a single argument and returns a result, possibly aPromiseinstance. Each function is called one after another waiting for resolving intermediate promises passing previous results to next functions.
It returns a function, which takes a single argument, which is used to start the chain.
const getParent = seq([
id => `/item/${id}`,
heya.io.get,
data => `/item/${data.parentId}`,
heya.io.get
]);
getParent(1).then(parent => console.log(parent));
getParent(2).then(parent => console.log(parent));