I thought await Promise.all(promises) would continue even if there was an error encountered, but I am finding this not to be the case. Am I doing something wrong or do I just not understand what await is supposed to do?
The code snippet below illustrates the problem. promiseSimulator will return the number passed unless it is 5, which will generate an error. The line of code after await Promise.all(promises) will execute only if I remove the code to generate the error. Otherwise, if an error is encountered, the await never completes. How can I get this to work correctly?
async runTest() {
const promises:Promise<number>[] = [];
for (let i = 0; i < 10; i++) {
const promise:Promise<number> = this.promiseSimulator(i);
promises.push(promise);
promise
.then( (response:number) => console.log('response: ', response))
.catch( (error:any) => console.log('error', error) );
}
console.log('begin await all');
await Promise.all(promises);
console.log('done await all');
}
private promiseSimulator(input:number):Promise<number> {
return new Promise<number>(
(resolve, reject) => {
setTimeout( () => {
if (input === 5) {
reject(new Error("Found a 5"));
}
else {
resolve(input);
}
}, 100)
}
);
}