10 examples of 'node js async each' in JavaScript

Every line of 'node js async each' 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
133function eachAsync(arr, eachFn, callback) {
134 arr = arr || [];
135
136 let idx = 0;
137 let awaiting = 0;
138 for (idx = 0; idx < arr.length; ++idx) {
139 awaiting++;
140 eachFn(arr[idx], eachCallback);
141 }
142
143 if (awaiting === 0) {
144 callback();
145 return;
146 }
147
148 function eachCallback(err) {
149 awaiting--;
150 if (err) {
151 callback(err);
152 return;
153 }
154
155 if (idx === arr.length && awaiting <= 0) {
156 callback();
157 }
158 }
159}
26async function asyncForEachAsync(iterable: AsyncIterable, func: AsyncIteratorFunction): Promise {
27 let index = 0;
28
29 for await (const item of iterable) {
30 const returnValue = func(item, index++);
31
32 if (returnValue instanceof Promise) {
33 await returnValue;
34 }
35 }
36}
149exports.async = function asyncFunction(iter) {
150 return new Promise((resolve, reject) => {
151 resume('next', undefined);
152 function resume(type, value) {
153 try {
154 let result = iter[type](value);
155 if (result.done) {
156 resolve(result.value);
157 } else {
158 Promise.resolve(result.value).then(
159 x => resume('next', x),
160 x => resume('throw', x));
161 }
162 } catch (x) {
163 reject(x);
164 }
165 }
166 });
167};
78function eachAsync(arr, fn, finalFunc, chunkSize, i) {
79 i = i || 0;
80 chunkSize = chunkSize || CHUNK_SIZE;
81
82 var threshold = i + chunkSize,
83 proceed = true;
84
85 while (proceed && i < threshold && i < arr.length) {
86 proceed = fn(arr[i], i);
87 i = i + 1;
88 }
89 if (proceed) {
90 if (i < arr.length) {
91 setTimeout(function () {
92 eachAsync(arr, fn, finalFunc, chunkSize, i);
93 });
94 } else if (finalFunc) {
95 finalFunc();
96 }
97 }
98}
13function forEachAsync(arr, fn, thisArg) {
14 var result = PromiseA.resolve()
15 ;
16
17 arr.forEach(function (item, k) {
18 result = result.then(function () {
19
20 var ret
21 ;
22
23 if (thisArg) {
24 ret = fn.call(thisArg, item, k, arr);
25 } else {
26 ret = result = fn(item, k, arr);
27 }
28
29 if (!ret || !ret.then) {
30 ret = PromiseA.resolve(ret);
31 }
32
33 return ret.then(function (val) {
34 if (val === forEachAsync.__BREAK) {
35 return PromiseA.reject(new Error('break'));
36 //throw new Error('break');
37 }
38
39 return val;
40 });
41 });
42 });
43
44 result.catch(function (e) {
45 if ('break' !== e.message) {
46 throw e;
47 }
48 });
49
50 return result;
51}
12function AsyncIterator(array, step, start, end){
13 this.array = array;
14 this.currentIndex = -1;
15 this.stepFn = step || EmptyFunction;
16 this.startFn = start || EmptyFunction;
17 this.endFn = end || EmptyFunction;
18}
561function asyncForEach(array, handler, callback) {
562 var length = array.length,
563 index = -1;
564
565 function processNext(err) {
566 if (err) return callback(err);
567 index++;
568 if (index < length) {
569 handler(array[index], processNext, index);
570 }
571 else if (callback) {
572 typeof setImmediate === 'function' ? setImmediate(callback) : setTimeout(callback, 0);
573 }
574 }
575
576 processNext();
577}
163function asyncForEach(list, iterator, done) {
164 var i = 0;
165 function loop(error, index) {
166 if (index && index > i) {
167 i = index;
168 }
169 if (error || i >= list.length) {
170 done(error);
171 } else {
172 try {
173 iterator(list[i++], loop);
174 } catch (e) {
175 done(e);
176 }
177 }
178 }
179 loop();
180}
11function next() {
12
13 n--;
14 callback(target[n], n);
15 if(n) process.nextTick(next);
16}
47module.exports.eachSeries = function eachSeries(arr, fun) {
48 return arr.reduce((p, e) => p.then(() => fun(e)), Promise.resolve());
49};

Related snippets