-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
46 lines (45 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const Sum = require('apr-engine-sum');
const reduce = require('apr-reduce');
/**
* <a id="series"></a>
* Run the functions in the `tasks` in series, each one running once the previous function has completed.
*
* [![](https://img.shields.io/npm/v/apr-series.svg?style=flat-square)](https://www.npmjs.com/package/apr-series) [![](https://img.shields.io/npm/l/apr-series.svg?style=flat-square)](https://www.npmjs.com/package/apr-series)
*
* @kind function
* @name series
* @param {Array<Function>|Object} tasks
* @returns {Promise}
*
* @example
* import series from 'apr-series';
*
* const then = (v) => new Promise((resolve) => resolve(v));
*
* const withArray = await series([
* async () => await then(1),
* async () => await then(2)
* ]);
*
* // withArray = [1, 2]
*
* const withObject = await series({
* one: async () => await then(1),
* two: async () => await then(2)
* });
*
* // withObject = { one: 1, two: 2 }
*/
module.exports = input =>
reduce(
input,
(sum, fn, key) =>
fn().then(res => {
sum[key] = res;
return sum;
}),
Sum(input),
{
limit: 1
}
);