what does Promise.all does code example
Example 1: javascript promise.all
try {
const [res1, res2, res3] = await Promise.all([
Promise1,
Promise2,
Promise3,
]);
} catch (err) {
console.error(err);
}
Example 2: promise.all
// A simple promise that resolves after a given time
const timeOut = (t) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(`Completed in ${t}`)
}, t)
})
}
// Resolving a normal promise.
timeOut(1000)
.then(result => console.log(result)) // Completed in 1000
// Promise.all
Promise.all([timeOut(1000), timeOut(2000)])
.then(result => console.log(result)) // ["Completed in 1000", "Completed in 2000"]