【发布时间】:2018-11-12 02:05:07
【问题描述】:
在下面的代码中,我使用forEach 并在其中迭代一个数组,在forEach 中我调用另一个函数,同时将参数作为callback 传递,现在事情甚至我都在调用它函数的回调,forEach 回调永远不会被调用
async.forEach(Array_Ids, function (item, callback){
sendPushNotif(item.mess, item.Ids, callback);
}, function(err) {
// EXECUTION NEVER COMING HERE
if(err) {
res.json({status_code : 200, message : "Correctly Hit the URL!"});
return next();
} else {
res.json({status_code : 200, message : "Correctly Hit the URL!"});
return next();
}
});
function sendPushNotif(mess, Ids, callback) {
sender.send(mess, { registrationTokens: Ids }, function(err, result) {
if(err) {
callback(null);
}
else {
console.log(null);
}
});
}
【问题讨论】:
-
我认为
sendPushNotif不会出于某种原因调用callback。尝试以相同的方式包装回调sendPushNotif((err, res) => {console.log('Done', item); callback(err, res);});。 -
@PatrickRoberts 你能再看一遍吗,谢谢。
标签: javascript node.js callback async.js