Skip to content

Instantly share code, notes, and snippets.

@abhiche
Created October 17, 2017 16:01
Show Gist options
  • Save abhiche/9d99982a811e1eacb69d2e546d0f1758 to your computer and use it in GitHub Desktop.
Save abhiche/9d99982a811e1eacb69d2e546d0f1758 to your computer and use it in GitHub Desktop.
// Copied from SO
/**
* @param {Promise} promise
*/
function reflect(promise) {
return promise
.then(d => {
return {
d: d,
status: 'resolved'
};
}).catch(e => {
return {
e: e,
status: 'rejected'
};
});
}
/**
* @param {Array} promiseList
* Executes all the promises even if one of them fails
* Final result is in resolution, result[i][status] represents rejection or resolution of each promise
*/
function execAllPromises(promiseList) {
return Promise.all(promiseList.map(reflect));
}
// Usage
execAllPromises([
fetch('https://en.wikipedia.org/api/rest_v1/page/'),
fetch('https://en.wikipedia.org/api/rest_v1/page/title/')
]).then(result => console.log(result));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment