Home  >  Article  >  Web Front-end  >  Does Promise.all Preserve the Order of Resolved Values?

Does Promise.all Preserve the Order of Resolved Values?

Barbara Streisand
Barbara StreisandOriginal
2024-11-04 12:15:02235browse

Does Promise.all Preserve the Order of Resolved Values?

Order of Resolved Values in Promise.all

The Promise.all method takes an iterable as an argument and returns a single promise that resolves to an array of values once all the promises in the iterable have resolved. The documentation suggests that the order of values in the resolved array is preserved, but is there a more authoritative source?

Order Preservation in the Specification

The ECMAScript specification for Promise.all states that:

All Promise instances in promiseList are resolved and their respective elements in the result list are set to the corresponding values.

This suggests that the order of the promise instances in the input iterable is preserved in the output array.

Internal Implementation

To resolve a promise, the Promise.all() method uses the Promise.all().Resolve internal operation. This operation assigns an internal [[Index]] slot to each promise instance, indicating its index in the original input iterable.

Conclusion

Based on the specification and the internal implementation of Promise.all(), we can confidently say that the order of resolved values is preserved. The resolved array strictly follows the order of the promises in the input iterable.

The above is the detailed content of Does Promise.all Preserve the Order of Resolved Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn