'javascript - unable to get response from promise.all()
I find that I cannot get the response when using promise.all.
The orders below should be an array
Promise.all(promises).then(function (orders) {
console.log(orders); // nothing logged
});
Full code
let promises = markets.map(async (mkt) => {
return new Promise(async function (resolve, reject) {
return functionA(paramA)
.then(resA => {
return res.functionB()
.then((resB) => {
if (resB.length > 0) {
return functionC()
.then(resC => {
console.log(resC); // successfully logged
return resC;
});
}
});
})
.catch(err => console.log(err));
});
});
Promise.all(promises).then(function (orders) {
console.log(orders); // nothing logged
});
How can I fix?
Update 1
I update the code based on the comment.
The orders is now returning
(54) [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, Array(1), undefined, undefined, undefined, undefined, undefined, undefined, undefined, Array(1), undefined, Array(1), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
How can I make the promise returns without undefined?
const promises = markets.map(async mkt => {
const resA = await functionA(paramA);
const resB = await res.functionB();
if (resB.length > 0) {
const resC = await functionC()
console.log(resC); // successfully logged
return resC;
}
});
Promise.all(promises).then(orders => {
console.log(orders); // nothing logged
}, err => {
console.log(err);
});
Solution 1:[1]
- The Promise constructor ignores any value returned from its callback
- You aren't calling
resolveanywhere - You don't need to construct a Promise when you're calling a function that already gives you a Promise
const promises = markets.map((mkt) => {
return functionA(paramA)
.then(resA => res.functionB())
.then((resB) => {
if (resB.length > 0) {
return functionC()
.then(resC => {
console.log(resC); // successfully logged
return resC;
});
}
})
.catch(handleError);
});
A better approach would be to utilize await to make the code flat and easy to read.
const promises = markets.map(async (mkt) => {
try {
const resA = await functionA(paramA);
const resB = await res.functionB();
if (resB.length > 0) {
const resC = await functionC()
return resC;
}
} catch (e) {
// handle errors
}
});
You also might consider handling errors in the Promise.all, not in the mapper.
If you want to include only items for which functionC is called, filter out the empty values afterwards.
Promise.all(promises).then(function (orders) {
const filtered = orders.filter(Boolean);
console.log(filtered);
});
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 |
