【发布时间】:2018-08-27 17:46:17
【问题描述】:
我在 node.js 中有一个 lambda 函数来发送推送通知。
在该函数中,我需要遍历我的用户,在回调之前为每个用户发送通知。
理想情况下,我希望迭代并行执行。
最好的方法是什么?
我的代码目前如下,但它没有按预期工作,因为最后一个用户并不总是最后一个被处理的用户:
var apnProvider = new apn.Provider(options);
var iterationComplete = false;
for (var j = 0; j < users.length; j++) {
if (j === (users.length - 1)) {
iterationComplete = true;
}
var deviceToken = users[j].user_device_token;
var deviceBadge = users[j].user_badge_count;
var notification = new apn.Notification();
notification.alert = message;
notification.contentAvailable = 1;
notification.topic = "com.example.Example";
apnProvider.send(notification, [deviceToken]).then((response) => {
if (iterationComplete) {
context.succeed(event);
}
});
}
【问题讨论】:
标签: node.js parallel-processing aws-lambda iteration async.js