[英]javascript - unable to get response from promise.all()
I find that I cannot get the response when using promise.all
.我发现使用
promise.all
时无法得到响应。
The orders
below should be an array下面的
orders
应该是一个数组
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更新 1
I update the code based on the comment.我根据评论更新代码。
The orders
is now returning orders
现在正在返回
(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
?如何使 promise 返回没有
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);
});
resolve
anywhereresolve
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.更好的方法是利用
await
使代码平坦且易于阅读。
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.您还可以考虑在
Promise.all
中处理错误,而不是在映射器中。
If you want to include only items for which functionC
is called, filter out the empty values afterwards.如果您只想包含调用
functionC
的项目,请在之后过滤掉空值。
Promise.all(promises).then(function (orders) {
const filtered = orders.filter(Boolean);
console.log(filtered);
});
Well, you never resolve()
or reject()
the new Promise
s you're creating.好吧,您永远不会
resolve()
或reject()
您正在创建的new Promise
。 Avoid the Promise
constructor antipattern and never pass an async function
as the executor to new Promise
!避免使用
Promise
构造函数反模式,并且永远不要将async function
作为执行者传递给new Promise
!
This code can be greatly simplified by consequent usage of async
/ await
instead of .then()
:通过随后使用
async
/ await
而不是.then()
可以大大简化此代码:
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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.