我对Parse javascript代码和承诺都非常陌生。
我的代码的目标是向数据库中每个竞技场的2名玩家发送通知,如果通知发送错误,则保持继续。
var query = new Parse.Query('arena');
query.each((arena) => {
return NotificationServices.sendNotification(getPlayerOne(arena))
.then(() => {
return NotificationServices.sendNotification(getPlayerTwo(arena))
})
.then(() => Parse.Promise.as(), () => Parse.Promise.as()); // Continue if notification send error.
})
如何更改代码,以便异步执行2个任务sendNotification?
提前谢谢。
发布于 2016-06-08 11:22:42
如果您只想执行它们异步,只需调用它们而不需要".then“。
如果你想得到这两个完成的承诺,或者其中一个失败了。您可以使用Parse.Promise.when。Parse.Promise.when离es6 Promise.all很近。
试试这个
var query = new Parse.Query('arena');
query.each((arena) => {
var promise1 = NotificationServices.sendNotification(getPlayerOne(arena));
var promise2 = NotificationServices.sendNotification(getPlayerTwo(arena));
return Parse.Promise.when([promise1, promise2])
.then(() => Parse.Promise.as(), () => Parse.Promise.as());
})https://stackoverflow.com/questions/37699795
复制相似问题