7 examples of 'async waterfall' in JavaScript

Every line of 'async waterfall' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
45function _waterfall (steps, done) {
46 function next () {
47 var d = once(done);
48 return once(function callback () {
49 var args = atoa(arguments);
50 var step = steps.shift();
51 if (step) {
52 if (handle(args, d)) { return; }
53 args.push(next());
54 cb(step, args);
55 } else {
56 cb(d, arguments);
57 }
58 });
59 }
60 next()();
61}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

109function waterfall (steps) {
110 return topCallback => {
111 const callbackers = steps.map((step, index) => {
112 return index === 0
113 ? ensureValue(step)
114 : (value, callback) => ensureValue(step(value))(callback)
115 })
116 runWaterfall(callbackers, topCallback)
117 }
118
119 // ensure value argument is passed to callback
120 function ensureValue (continuable) {
121 return function callbacker (callback) {
122 continuable((err, value) => {
123 callback(err, value)
124 })
125 }
126 }
127}
23export function asyncMap<T, T2>(array: T[], asyncFn: (x: T) => T2): Promise<T2[]> {
24 return Promise.all(array.map(asyncFn));
25}
16function asyncMap(arr, func, done) {
17 var results = [],
18 length = arr.length,
19 i = 0;
20
21 if (!length) task(function () { done(results); });
22 else {
23 forEach(arr, function (item, index) {
24 task(function () {
25 func(item, function (result) {
26 i++;
27 results[index] = result;
28 if (i == length) done(results);
29 });
30 });
31 });
32 }
33}
38static try(asyncVal) {
39 return asyncVal;
40}
113export function promiseSeries(
114 tasks: Function[],
115 initial?: any
116): Promise<any[]> {
117 return tasks
118 .reduce(
119 (current, next) => (current as any).then(next),
120 Promise.resolve(initial)
121 );
122}
6export async function allAsync<TSource>(
7 source: AsyncIterable<TSource>,
8 predicate: (x: TSource) => Promise<boolean>): Promise<boolean> {
9 for await (const item of source) {
10 if (await predicate(item) === false) {
11 return false
12 }
13 }
14
15 return true
16}

Related snippets