'Promise.all is not resolving if second promise is added to promise array
Given that promise1 and promise2 are correctly resolved with the below code:
export const getExistingPayments = async (payments: Payment[]) => {
const promise1 = await getPayment(payments[0].paymentId)
const promise2 = await getPayment(payments[1].paymentId)
const results = [promise1, promise2]
return results
}
Can anybody help explain why the below code would just hang, the promises are never resolved or rejected:
export const getExistingPayments = async (payments: Payment[]) => {
const promise1 = getPayment(payments[0].paymentId)
const promise2 = getPayment(payments[1].paymentId)
const results = await Promise.all([promise1, promise2])
return results
}
It might also be worth mentioning that when only one promise is passed to the Promise.all the promise is resolved as expected, the below code also works fine:
export const getExistingPayments = async (payments: Payment[]) => {
const promise1 = getPayment(payments[0].paymentId)
const results = await Promise.all([promise1)
return results
}
Solution 1:[1]
Try something like this, instead of assigning the result of the functions to a variables
const [results1, result2] = await Promise.all([
getPayment(payments[0].paymentId),
getPayment(payments[1].paymentId)
])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
