[英]How to wait for multiple asynchronous calls from forEach loop?
Trying to make call to multiple asynchronous function, and due to which getting result as undefined
. 尝试调用多个异步函数,并且由于该原因导致结果为
undefined
。
Tried async.waterfall
, however not able to make it work. 尝试了
async.waterfall
,但是无法使其工作。
Code: 码:
const pendingData = [];
async.waterfall([
function (callback) {
WaitForApproval.find({}, (err,result) => {
callback(null,result);
});
},
function (result, callback) {
result.forEach(data => {
User.findOne({_id: data.uploadedBy}, (err,name) => {
let total = {
id: data._id,
name: name.name,
subject: data.subject,
uploadOn: data.uploadedAt
};
pendingData.push(total);
});
});
callback(null,'done');
}
], function (err,result) {
if(result === 'done') {
console.log(pendingData); // it is giving empty result.
}
});
How to wait for asynchronous function? 如何等待异步功能?
The issue you are having is that you are async functions within a non-async forEach loop. 您遇到的问题是您是非异步forEach循环中的异步函数。
You have a few options here: 您可以在此处选择以下几种方式:
Make the mongoDB call recursively - wrap this query in a function that calls itself after the query returns. 递归进行mongoDB调用-将查询包装在查询返回后自动调用的函数中。
Read about mongoDB batch operations - https://docs.mongodb.com/manual/reference/method/Bulk/ 阅读关于MongoDB的批量操作- https://docs.mongodb.com/manual/reference/method/Bulk/
Use an async/await pattern with each call by declaring the callback function within the async waterfall as 'async' and then using 'await' inside of the function for each query. 通过在异步瀑布中将回调函数声明为“ async”,然后在每个查询的函数内部使用“ await”,对每个调用使用async / await模式。 Out of the box, forEach is not async.
开箱即用,forEach不异步。 If you want to still use forEach, you can either re-write it async (See below) or use a regular for loop:
如果仍要使用forEach,则可以将其异步重写(请参见下文),也可以使用常规的for循环:
async function asyncForEach(array, callback) { for (let index = 0; index < array.length; index++) { await callback(array[index], index, array) } }
** There are additional ways to solve this beyond what is posted here, but here's a few things that will work if implemented correctly. **除了此处发布的内容以外,还有其他方法可以解决此问题,但是如果正确实施,这里有一些方法会起作用。
I would suggest you add the call for callback(null,'done');
我建议您添加对
callback(null,'done');
的调用callback(null,'done');
immediately pendingData.push(total);
立即
pendingData.push(total);
you are right, the async nature is making things hard for you now, but suppose you used Promises and chain them together, that would save you a lot of trouble. 没错,异步的性质正在使您现在变得困难,但是假设您使用Promises并将它们链接在一起,那将为您节省很多麻烦。
once a time I had a similar problem of asynchronous code running out of order I wanted so I made a little Tweak using a custom promise function(I mead up) and call it order..such Idea can solve your problem if you could apply it to your code properly https://github.com/lalosh/Ideas/blob/master/promiseOrder.js 一次,我遇到了类似的异步代码失灵问题,因此我使用自定义的promise函数(我进行了调整)进行了一些微调,并将其命名为order..Idea可以解决您的问题,如果您可以应用它正确地输入代码https://github.com/lalosh/Ideas/blob/master/promiseOrder.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.