I'm trying to see myself the different between Promise.all
and await Promise.all
. I have learnt that the first one ends earlier if one of the promises fail but in the same case with await we have to wait to the completion of all promises.
In my example, in both cases, they finish at the same time. What I'm doing wrong?
/**
* Create a promise
* @param ok true -> resolve, false -> reject
*/
function create_promise(ok) {
return new Promise((resolve, reject) => {
setTimeout(() => ok ? resolve() : reject(), 2e3)
})
}
// Finish as soon as I get a reject
Promise.all([create_promise(true), create_promise(false), create_promise(true)])
.catch(() => console.log('rejected'))
// Finish after waiting all
const my_async_function = async () =>
await Promise.all([create_promise(true), create_promise(false), create_promise(true)])
my_async_function().catch(() => console.log('rejected'))