Last active
June 25, 2018 08:06
-
-
Save guillaumegarcia13/09c95181bbc826b1bf7b191c79b4f782 to your computer and use it in GitHub Desktop.
Promises.all
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
// Play it on Typescript Playground: https://www.typescriptlang.org/play/ | |
class Test { | |
static process(): Promise<any> { | |
let promises = [ | |
Promise.resolve(1), | |
Promise.resolve(2), | |
Promise.reject(3), | |
Promise.resolve(4), | |
new Promise((res, rej) => { throw new Error('Boum'); }), | |
Promise.resolve(6), | |
new Promise((res, rej) => setTimeout(() => { res(7); }, 1000)), | |
Promise.resolve(8), | |
]; | |
return Promise | |
.all(promises.map(p => p | |
.then((v: number) => { return { result: v }; }) | |
.catch((err: any) => { return { error: err }; }) | |
)) | |
.then((values: { result?: number, error?: any }[]) => { | |
console.log(values); | |
}) | |
.catch((err: any) => { throw err; }); | |
} | |
static run() { | |
console.log('Process started'); | |
Test.process() | |
.then(() => { console.log('Process completed'); }); | |
} | |
} | |
Test.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment