Created
October 17, 2017 16:01
-
-
Save abhiche/9d99982a811e1eacb69d2e546d0f1758 to your computer and use it in GitHub Desktop.
This file contains 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
// 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