10 examples of 'bluebird promise.each' in JavaScript

Every line of 'bluebird promise.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
this disclaimer
181function all(promises) {
182 /*jshint maxcomplexity:8*/
183 var resolver = new Pending();
184 var pending = promises.length >>> 0;
185 var results = new Array(pending);
186
187 var i, h, x, s;
188 for (i = 0; i < promises.length; ++i) {
189 x = promises[i];
190
191 if (x === void 0 && !(i in promises)) {
192 --pending;
193 continue;
194 }
195
196 if (maybeThenable(x)) {
197 h = getHandlerMaybeThenable(x);
198
199 s = h.state();
200 if (s === 0) {
201 h.fold(settleAt, i, results, resolver);
202 } else if (s > 0) {
203 results[i] = h.value;
204 --pending;
205 } else {
206 unreportRemaining(promises, i+1, h);
207 resolver.become(h);
208 break;
209 }
210
211 } else {
212 results[i] = x;
213 --pending;
214 }
215 }
216
217 if(pending === 0) {
218 resolver.become(new Fulfilled(results));
219 }
220
221 return new Promise(Handler, resolver);
222
223 function settleAt(i, x, resolver) {
224 /*jshint validthis:true*/
225 this[i] = x;
226 if(--pending === 0) {
227 resolver.become(new Fulfilled(this));
228 }
229 }
230}
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

92function chainPromise(promises) {
93 var chain = Promise.resolve()
94 promises.forEach(function (promise) {
95 chain = chain.then(function (o) {
96 return typeof promise === 'function' ? promise(o) : promise
97 })
98 })
99 return chain
100}
31Promise.each = function Promise$Each(promises, fn) {
32 return PromiseReduce(promises, fn, null, INTERNAL);
33};
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}
162return new Promise(function promiseFn(resolve, reject) {
163 resolve(response);
164 reject('mock request failed');
165});
37public static whenAll(promises: Promise[], action: () => void) {
38 const pending = promises.filter(p => !p.isDone);
39 if (pending.length === 0) {
40 action();
41 }
42 else {
43 pending.forEach(p => {
44 p.whenDone.push(() => {
45 const index = pending.indexOf(p);
46 if (index >= 0)
47 pending.splice(index, 1);
48 if (pending.length === 0)
49 action();
50 });
51 });
52 }
53}
169Promise.all = function all(promiseList)
170{
171 var ret = new Promise(function (res, rej)
172 {
173 this.__rejector = rej;
174 this.__resolver = res;
175 this.__promiseList = promiseList;
176 this.__done = false;
177 this.__count = 0;
178 });
179
180 for (var i in promiseList)
181 {
182 promiseList[i].then(function ()
183 {
184 // Success
185 if(++ret.__count == ret.__promiseList.length)
186 {
187 ret.__done = true;
188 ret.__resolver(ret.__promiseList);
189 }
190 }, function (arg)
191 {
192 // Failure
193 if(!ret.__done)
194 {
195 ret.__done = true;
196 ret.__rejector(arg);
197 }
198 });
199 }
200 if (promiseList.length == 0)
201 {
202 ret.__resolver(promiseList);
203 }
204 return (ret);
205};
48async function parallel (promises) {
49 return await B.all(promises);
50}
29export function promiseOr(promises: Array<Promise<any>>): Promise<any> {
30 let resolved = false;
31 let rejected = 0;
32 return new Promise((resolve, reject) => {
33 for (const promise of promises) {
34 promise.then((res) => {
35 if (!resolved) {
36 resolved = true;
37 resolve(res);
38 }
39 }).catch(() => {
40 rejected++;
41 if (!resolved && rejected >= promises.length) reject();
42 });
43 }
44 });
45}
36export function eachParallelAndFinish<T, U>(arr: T[], iteratorFn: (arg: T) => Promise<U>): Promise<(U | Error)[]> {
37 const result: (U | Error)[] = [];
38 let itemsLeft = arr.length;
39 return new Promise((resolve) => {
40 arr.forEach((item: T, i: number) => {
41 iteratorFn(item).then((val: U) => {
42 result[i] = val;
43 if (--itemsLeft === 0) {
44 return resolve(result);
45 }
46 }).catch((err: Error) => {
47 result[i] = err;
48 if (--itemsLeft === 0) {
49 return resolve(result);
50 }
51 });
52 });
53 });
54}

Related snippets