Created
January 13, 2017 18:44
-
-
Save juanmav/93c6cd558c647d9cfba71259e914da24 to your computer and use it in GitHub Desktop.
No fail fast promise solution
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
| let p1 = new Promise((resolve, reject) => { | |
| setTimeout(resolve, 1000, "Everything OK in Promise 1"); | |
| }); | |
| let p2 = new Promise((resolve, reject) => { | |
| setTimeout(resolve, 2000, "Everything OK in Promise 2"); | |
| }); | |
| let p3 = new Promise((resolve, reject) => { | |
| setTimeout(reject, 2500, new Error("Something went wrong in Promise 3!")); | |
| }); | |
| const toResultObject = (promise) => { | |
| return promise | |
| .then(result => ({ success: true, result })) | |
| .catch(error => ({ success: false, error })); | |
| }; | |
| Promise.all([p1, p2, p3].map(toResultObject)).then((values) => { | |
| for (let i = 0; i < values.length; ++i) { | |
| if (!values[i].success) { | |
| console.log("ERR: " + values[i].error); | |
| } else { | |
| console.log(values[i].result); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment