TL;DR
Use Promise.all
for the parallel function calls, the answer behaviors not correctly when the error occurs.
First, execute all the asynchronous calls at once and obtain all the Promise
objects. Second, use await
on the Promise
objects. This way, while you wait for the first Promise
to resolve the other asynchronous calls are still progressing. Overall, you will only wait for as long as the slowest asynchronous call. For example:
// Begin first call and store promise without waiting
const someResult = someCall();
// Begin second call and store promise without waiting
const anotherResult = anotherCall();
// Now we await for both results, whose async processes have already been started
const finalResult = [await someResult, await anotherResult];
// At this point all calls have been resolved
// Now when accessing someResult| anotherResult,
// you will have a value instead of a promise
JSbin example: http://jsbin.com/xerifanima/edit?js,console
Caveat: It doesn't matter if the await
calls are on the same line or on different lines, so long as the first await
call happens after all of the asynchronous calls. See JohnnyHK's comment.
Update: this answer has a different timing in error handling according to the @bergi's answer, it does NOT throw out the error as the error occurs but after all the promises are executed.
I compare the result with @jonny's tip: [result1, result2] = Promise.all([async1(), async2()])
, check the following code snippet
const correctAsync500ms = () => {_x000D_
return new Promise(resolve => {_x000D_
setTimeout(resolve, 500, 'correct500msResult');_x000D_
});_x000D_
};_x000D_
_x000D_
const correctAsync100ms = () => {_x000D_
return new Promise(resolve => {_x000D_
setTimeout(resolve, 100, 'correct100msResult');_x000D_
});_x000D_
};_x000D_
_x000D_
const rejectAsync100ms = () => {_x000D_
return new Promise((resolve, reject) => {_x000D_
setTimeout(reject, 100, 'reject100msError');_x000D_
});_x000D_
};_x000D_
_x000D_
const asyncInArray = async (fun1, fun2) => {_x000D_
const label = 'test async functions in array';_x000D_
try {_x000D_
console.time(label);_x000D_
const p1 = fun1();_x000D_
const p2 = fun2();_x000D_
const result = [await p1, await p2];_x000D_
console.timeEnd(label);_x000D_
} catch (e) {_x000D_
console.error('error is', e);_x000D_
console.timeEnd(label);_x000D_
}_x000D_
};_x000D_
_x000D_
const asyncInPromiseAll = async (fun1, fun2) => {_x000D_
const label = 'test async functions with Promise.all';_x000D_
try {_x000D_
console.time(label);_x000D_
let [value1, value2] = await Promise.all([fun1(), fun2()]);_x000D_
console.timeEnd(label);_x000D_
} catch (e) {_x000D_
console.error('error is', e);_x000D_
console.timeEnd(label);_x000D_
}_x000D_
};_x000D_
_x000D_
(async () => {_x000D_
console.group('async functions without error');_x000D_
console.log('async functions without error: start')_x000D_
await asyncInArray(correctAsync500ms, correctAsync100ms);_x000D_
await asyncInPromiseAll(correctAsync500ms, correctAsync100ms);_x000D_
console.groupEnd();_x000D_
_x000D_
console.group('async functions with error');_x000D_
console.log('async functions with error: start')_x000D_
await asyncInArray(correctAsync500ms, rejectAsync100ms);_x000D_
await asyncInPromiseAll(correctAsync500ms, rejectAsync100ms);_x000D_
console.groupEnd();_x000D_
})();
_x000D_