Created
April 5, 2018 19:53
-
-
Save xemasiv/bee7b5ee0afebbafd511b3e9c40834bd to your computer and use it in GitHub Desktop.
IterablePromise - Promise.all from iterated entities without the headache.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const IterablePromise = (iterableArray, promiseExecutorFunction) => { | |
return Promise.all( | |
iterableArray.map((item, index) => { | |
return new Promise((resolve, reject)=>{ | |
promiseExecutorFunction(item, index, resolve, reject); | |
}); | |
}) | |
); | |
}; | |
// Usage: | |
const Persons = [ | |
{ name: 'Xema'}, | |
{ name: 'Siv'} | |
]; | |
IterablePromise(Persons, (item, index, resolve, reject) => { | |
console.log(item.name); | |
resolve(); | |
}) | |
.then(() => { | |
console.log('Done!'); | |
}) | |
.catch(console.error); | |
// Result: | |
// Xema | |
// Siv | |
// Done! |
Or turned into a queue using async.queue
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can be interchanged with Promise.race