-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.js.html
112 lines (84 loc) · 2.93 KB
/
series.js.html
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: series.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: series.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
/**
* Running multiple functions which depend on the output of
* the previous function. If an error is encountered in any
* of the tasks, no more functions are run but the final callback
* is called with the error value.
* @param {Array} fns - array of functions
* @param {Function} done - final callback
* @param {Boolean} isCb - config for functions,
* is functions has callback logic or async
*
* @example
* const createFn = value => (err, cb) => {
setTimeout(() => {
if (value === 6) return cb(new Error('sorry'));
else return cb(err, value);
}, value);
};
const fns = [createFn(2000), createFn(60), createFn(500)];
series(fns, (err, data) => {
const result = { err, data };
console.log(result);
}, { isCb: 1 });
*
*/
const series = function(fns, done, config = {}) {
const { isCb } = config;
const results = [];
const nextFn = async (fns, done) => {
if (!fns.length) return done(null, results);
const fn = fns.shift();
if (isCb) {
const callback = (err, result) => {
results.push(result);
if (err) return done(err);
else return nextFn(fns, done);
};
fn(null, callback);
} else {
try {
const result = await fn();
results.push(result);
return nextFn(fns, done);
} catch (err) {
return done(err);
}
}
};
nextFn(fns, done);
};
module.exports = { series };
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AsyncEmitter.html">AsyncEmitter</a></li></ul><h3>Global</h3><ul><li><a href="index.html#asyncMemoize">asyncMemoize</a></li><li><a href="index.html#map">map</a></li><li><a href="index.html#Queue">Queue</a></li><li><a href="index.html#queue">queue</a></li><li><a href="index.html#reduce">reduce</a></li><li><a href="index.html#retry">retry</a></li><li><a href="index.html#series">series</a></li><li><a href="index.html#some">some</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Jan 04 2021 15:21:06 GMT+0200 (GMT+02:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>