[英]How to wait for all promises to be resolved before continuing
I have this code 我有这个代码
async function concurrent() {
await Promise.all([
getColorCount('black'),
getColorCount('purple'),
getColorCount('red'),
getColorCount('amber'),
getColorCount('yellow'),
getColorCount('white'),
buildChartData()
]);
}
concurrent();
res.json(chartData); //This is run before above promises are complete
My understanding was that async await halted the code until complete, but in this case, res.json(chartData) runs before the above promises have been completed. 我的理解是,异步等待代码直到完成为止,但是在这种情况下,res.json(chartData)在上述承诺完成之前就运行了。
How do I ensure that all promises have completed before responding to the client with res.json(chartData);
在使用
res.json(chartData);
响应客户端之前,如何确保所有承诺均已完成res.json(chartData);
? ?
return
a value from the function, or use .then()
within concurrent()
function call 从函数
return
值,或在concurrent()
函数调用中使用.then()
function concurrent() {
return Promise.all([
getColorCount('black'),
getColorCount('purple'),
getColorCount('red'),
getColorCount('amber'),
getColorCount('yellow'),
getColorCount('white'),
buildChartData()
]);
}
concurrent()
.then(chartData =>
res.json(chartData)
)
.catch(err => console.error(err));
async function concurrent() {
const promises = await Promise.all([
getColorCount('black'),
getColorCount('purple'),
getColorCount('red'),
getColorCount('amber'),
getColorCount('yellow'),
getColorCount('white'),
buildChartData()
]);
return promises;
}
concurrent()
.then(chartData =>
res.json(chartData)
)
.catch(err => console.error(err));
Use 采用
concurrent().then(promiseResult => {
res.json(chartData);
});
An async function always returns a promise and hence using "then", you can ensure the completion of an async function in a synchronised way. 异步函数总是返回promise,因此使用“ then”,您可以确保异步函数以同步方式完成。
You will need .then
after promise.all. 在
.then
之后,您将需要。 Then will return an array of values returned from your function calls in order they were initially called. 然后将返回从函数调用中返回的值的数组,这些值按最初被调用的顺序排列。
Promise.all([ getColorCount('black'), getColorCount('purple'), getColorCount('red'), getColorCount('amber'), getColorCount('yellow'), getColorCount('white'), buildChartData() ]) .then( function( responeses ) { console.log ( responses )};
And take a look at the responses array, it should all fall into place 看看responses数组,它应该全部放到位
concurrent
is an asynchrounous function, as you indicate with the keyword async
. concurrent
是一个异步函数,正如您使用关键字async
指示的那样。 That's why res.json
is executed just after calling concurrent, without waiting. 这就是为什么在调用并发后
res.json
执行res.json
而不等待的原因。
You can be tempted to use await like this: 您可能会喜欢这样使用await:
await concurrent();
res.json(chartData);
but it will throw an error because await can't be used outside a async context. 但这会引发错误,因为await不能在异步上下文之外使用。
Unluckily you will need the traditional then
to fulfill the promise: 不幸你将需要传统的
then
履行承诺:
concurrent().then(() => {
res.json(chartData);
});
Apart from that, is buildChartData
asynchronous? 除此之外,
buildChartData
异步? Maybe it should be executed outside Promimse.all
也许应该在
Promimse.all
之外Promimse.all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.