await Promise.all
时间: 2023-10-20 08:26:36 浏览: 44
`await Promise.all` is a syntax used in JavaScript to wait for multiple promises to resolve or reject before continuing with the execution of code. It takes an array of promises and returns a new promise that resolves to an array of the resolved values of each promise in the input array, or rejects with the reason of the first promise that rejects.
For example, the following code waits for two promises to resolve before logging their results:
```
const promise1 = Promise.resolve(1);
const promise2 = Promise.resolve(2);
const [result1, result2] = await Promise.all([promise1, promise2]);
console.log(result1); // 1
console.log(result2); // 2
```
If any of the promises in the input array reject, the `Promise.all` call will reject with the reason of the first rejected promise:
```
const promise1 = Promise.resolve(1);
const promise2 = Promise.reject('Error');
try {
const [result1, result2] = await Promise.all([promise1, promise2]);
} catch (error) {
console.error(error); // Error
}
```