promise.allsetted
时间: 2023-11-10 10:51:57 浏览: 102
Promise.allSettled is a method in JavaScript that takes an iterable (like an array) of promises and returns a new promise. This new promise is fulfilled with an array of objects, each representing the outcome of each promise in the iterable. The objects in the array have two properties: "status" and "value".
The "status" property can have two values: "fulfilled" or "rejected", indicating whether the corresponding promise was resolved or rejected. The "value" property contains the resolved value of a fulfilled promise, or the reason for rejection in case of a rejected promise.
Here's an example usage of Promise.allSettled:
```javascript
const promises = [
Promise.resolve('Promise 1 resolved'),
Promise.reject('Promise 2 rejected'),
Promise.resolve('Promise 3 resolved')
];
Promise.allSettled(promises)
.then(results => {
console.log(results);
});
```
In this example, the `promises` array contains three promises. The first and third promises are resolved, while the second promise is rejected. Calling `Promise.allSettled(promises)` will return a new promise that resolves with an array of objects representing the outcome of each promise.
The `console.log(results)` statement will output the following array:
```javascript
[
{ status: 'fulfilled', value: 'Promise 1 resolved' },
{ status: 'rejected', reason: 'Promise 2 rejected' },
{ status: 'fulfilled', value: 'Promise 3 resolved' }
]
```
I hope this helps! Let me know if you have any more questions.
阅读全文