0

Cannot access value of resolved promise after Promise.all. As you see in the example below, res.prop prints undefined. I guess I could create a wrapping function that pushes the resolved value to another responses array but that does not seem like a clean way to do it.

(async () => {
    const responses = []

    let counter = 10
    while (counter--) {
      responses.push(new Promise(resolve => resolve({prop: 10})))
    }

    await Promise.all(responses)

    for (const res of responses) {
        console.log(res) // -> prints Promise {prop: 10}
        console.log(res.prop) // -> prints undefined
    }
})()
Dave
  • 457
  • 1
  • 7
  • 16
  • 2
    You never use the return value of `await Promise.all(responses)`. The return value of your promises is returned by `Promises.all` Edit : posted as answer for easier testing. – Seblor Mar 14 '19 at 15:08

1 Answers1

3

You never use the return value of await Promise.all(responses). The return value of your promises is returned by Promises.all :

(async () => {
    const responses = []

    let counter = 10
    while (counter--) {
      responses.push(new Promise(resolve => resolve({prop: 10})))
    }

    const results = await Promise.all(responses)

    for (const res of results) {
        console.log(res) // -> prints Promise {prop: 10}
        console.log(res.prop) // -> prints undefined
    }
})()
Seblor
  • 6,947
  • 1
  • 25
  • 46