promise.all

来源:undefined 2025-06-12 17:50:07 1001

Promise.all is a method in JavaScript that takes an array of promises as its argument and returns a new promise. This new promise is fulfilled with an array of values when all promises in the original array are fulfilled. If any of the promises in the array is rejected

the new promise is rejected.

One of the main use cases for Promise.all is when we want to execute multiple asynchronous operations and wait for all of them to complete before proceeding. It allows us to handle multiple promises in a more concise and efficient manner.

The syntax for Promise.all is as follows:

```

Promise.all(iterable);

```

The `iterable` parameter is an array (or any other iterable object) that contains the promises that we want to wait for. It can also include values that are not promises

in which case they are treated as immediately fulfilled promises.

Here is an example that demonstrates the basic usage of Promise.all:

```javascript

const promise1 = Promise.resolve(Hello);

const promise2 = Promise.resolve(World);

const promise3 = new Promise((resolve

reject) => {

setTimeout(resolve

2000

!);

});

const promise4 = Promise.reject(new Error(Error));

Promise.all([promise1

promise2

promise3])

.then(values => {

console.log(values); // Output: [Hello

World

!]

})

.catch(error => {

console.error(error); // Not executed since all promises are fulfilled

});

Promise.all([promise1

promise2

promise4])

.then(values => {

console.log(values); // Not executed since promise4 is rejected

})

.catch(error => {

console.error(error); // Output: Error: Error

});

```

In this example

we have four promises: `promise1`

`promise2`

`promise3`

and `promise4`. `promise1` and `promise2` are immediately fulfilled with the strings Hello and World respectively. `promise3` is a promise that is fulfilled after a delay of 2 seconds with the string !. `promise4` is a promise that is immediately rejected with an `Error` object.

We use `Promise.all` to wait for all promises to be fulfilled or rejected. In the first call to `Promise.all`

all promises are fulfilled

so the `then` callback is executed

and the values of the fulfilled promises are logged to the console.

In the second call to `Promise.all`

one of the promises (`promise4`) is rejected

so the `catch` callback is executed

and the error is logged to the console.

`Promise.all` returns a new promise that is fulfilled only when all promises in the `iterable` argument are fulfilled. If any of the promises is rejected

the new promise is immediately rejected. The values of the fulfilled promises are collected into an array

which is passed as an argument to the `then` callback. If no promise in the `iterable` argument is rejected

the new promise is fulfilled with the array of values.

Its worth noting that `Promise.all` works with any iterable object

not just arrays. This means that we can pass in a `Set`

a `Map`

or even a string as long as it implements the iterable protocol. However

in practice

arrays are the most commonly used iterable when working with `Promise.all`.

Another important aspect of `Promise.all` is that it preserves the order of the promises in the `iterable` argument. This means that the order of the values in the resulting array passed to the `then` callback reflects the order of the promises in the original array.

To take full advantage of `Promise.all`

its important to ensure that the promises in the `iterable` argument are all independent of each other. If there is any dependency between the promises

the code may not work as expected

and the ordering of the promises may have unintended consequences.

In conclusion

`Promise.all` is a powerful tool that allows us to handle multiple promises in a concise and efficient manner. It is particularly useful when we want to execute multiple asynchronous operations and wait for all of them to complete. By using `Promise.all`

we can simplify our code and improve its readability.

上一篇:mysql删除数据库 下一篇:rediszincrby命令

最新文章