Skip to content

Instantly share code, notes, and snippets.

@ppeeou
Created September 21, 2019 07:00
Show Gist options
  • Save ppeeou/c99c27c30fe250dbaf0e005f9561c801 to your computer and use it in GitHub Desktop.
Save ppeeou/c99c27c30fe250dbaf0e005f9561c801 to your computer and use it in GitHub Desktop.
// -- simple
function isIterator(obj) {
return !!obj && !!obj[Symbol.iterator];
}
function allSettled(iterable) {
if (!isIterator(iterable)) {
throw new Error('[allSettled] first param must be iterable');
}
const onFulfill = v => ({ status: 'fulfilled', value: v });
const onReject = v => ({ status: 'rejected', reason: v });
return Promise
.all(Array.from(iterable).map(p =>
Promise
.resolve(p)
.then(onFulfill)
.catch(onReject)));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment