[英]Waiting for multiple async calls using async.each
I need to wait for few async calls to complete before resuming execution.在恢复执行之前,我需要等待一些异步调用完成。 I tried the following code using async .我使用async尝试了以下代码。
asana.getUsers(null, function(error, resp){
async.each(resp.data, function(user, cb) {
console.log("Get info for user : ", user.id);
asana.getUser(user.id, null, function(error, userResp){
console.log("user response: ", userResp.data.id);
userEmailList.push({
id : userResp.data.id,
name: userResp.data.name,
email : userResp.data.email
});
cb(null);
});
//cb(null); Should the call be here??
}, function(err){
console.log("getUsers is done now");
});
});
The log that i get is:我得到的日志是:
Get info for user : xxxxxxxxxxxx Get info for user : yyyyyyyyyyyy Get info for user : zzzzzzzzzzzz Get info for user : aaaaaaaaaaaa user response: yyyyyyyyyyyy /Code/javaScript/NodeWorkspace/asana-api/mail.js:23 console.log("user response: ", userResp.data.id); ^ TypeError: Cannot read property 'data' of null at /Code/javaScript/NodeWorkspace/asana-api/mail.js:23:43 at Request._callback (/Code/javaScript/NodeWorkspace/asana-api/lib/asana.js:77:13) at Request.self.callback (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:119:22) at Request. (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:525:16) at Request.EventEmitter.emit (events.js:95:17) at IncomingMessage. (/Code/javaScript/NodeWorkspace/asana-api/node_modules/request/main.js:484:14) at IncomingMessage.EventEmitter.emit (events.js:117:20) at _stream_readable.js:883:14 at process._tickCallback (node.js:415:13)
I understand the first logs, but why is the console.log("user response: ", userResp.data.id);
我理解第一个日志,但为什么是console.log("user response: ", userResp.data.id);
line getting called with userResp
as null
.使用userResp
作为null
调用的行。
I hope its not some silly mistake.我希望这不是一些愚蠢的错误。 Thanks in advance.提前致谢。
PS: Basically i need to get Asana users and then get info of each user and store in a var userEmailList
. PS:基本上我需要获取 Asana 用户,然后获取每个用户的信息并存储在 var userEmailList
。
问题是userEmailList
未定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.