0

If I have the following code that returns a response in an unreliable way (i.e., you're never sure which one will complete faster), will that change the assignment of the variables?

Example:

const [response1, response2] = await Promise.all([request1, request2])

Will response1 always contain the response of request1, or is it possible that if request2 resolves faster it would get assigned to the first variable declared? You can assume for the sake of the code example that request1 and request2 are standard fetch() calls to an API.

yoursweater
  • 1,883
  • 3
  • 19
  • 41

2 Answers2

1

Will using the spread operator with Promise.All() get different results depending on which request resolves faster?

No. Promise.all() collects all the results and presents all the results to you in the order you put the promises into the array. It does not matter which request finishes first.

The one caveat is if any promise rejects, then Promise.all() will immediately report that rejection without waiting for the other promises.

If I have the following code that returns a response in an unreliable way (i.e., you're never sure which one will complete faster), will that change the assignment of the variables?

No. It will not change the order of the results. Promise.all() preserves the ordering of results for you. That's one of its features.

Will response1 always contain the response of request1

Yes.

or is it possible that if request2 resolves faster it would get assigned to the first variable declared?

No.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

The main question

Will response1 always contain the response of request1, or is it possible that if request2 resolves faster it would get assigned to the first variable declared?

The MDN page for Promise.all says the following:

Return value

...

A pending Promise in all other cases.

This returned promise is then resolved/rejected asynchronously (as soon as the stack is empty) when all the promises in the given iterable have resolved, or if any of the promises reject. See the example about "Asynchronicity or synchronicity of Promise.all" below. Returned values will be in order of the Promises passed, regardless of completion order.

As the returned array is in order, when it is unpacked it is unpacked in order.

The misunderstanding

The order that the promises are executed is not guaranteed as radarbob points out, but the return order is.

Example

Code

function sleepPrintReturn(value) {
    return new Promise((resolve, reject) => {
        const delay = Math.random() * 1000

        setTimeout(() => {
            console.log("Completed", value)
            resolve(value)
        }, delay)
    })   
}

(async () => {
    const [a,b,c] = await Promise.all([
        sleepPrintReturn("a"),
        sleepPrintReturn("b"),
        sleepPrintReturn("c")
    ])

    console.log("-------------")
    console.log(a)
    console.log(b)
    console.log(c)
})()

Runs

1

Completed c
Completed b
Completed a
-------------
a
b
c

2

Completed b
Completed a
Completed c
-------------
a
b
c
jrtapsell
  • 6,719
  • 1
  • 26
  • 49