Skip to content

Instantly share code, notes, and snippets.

@xemasiv
Created April 5, 2018 19:53
Show Gist options
  • Save xemasiv/bee7b5ee0afebbafd511b3e9c40834bd to your computer and use it in GitHub Desktop.
Save xemasiv/bee7b5ee0afebbafd511b3e9c40834bd to your computer and use it in GitHub Desktop.
IterablePromise - Promise.all from iterated entities without the headache.
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!
@xemasiv
Copy link
Author

xemasiv commented Apr 5, 2018

Can be interchanged with Promise.race

@xemasiv
Copy link
Author

xemasiv commented Apr 5, 2018

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