Parse JavaScript SDK and Promise Chaining

瘦欲@ 提交于 2019-11-28 10:35:31
lara

I'd clean it up as follows, relying on Promise.when ...

var savePromises = [];  // this will collect save promises 
var emailPromises = [];  // this will collect email promises 

// your code to setup the query here
// notice that this uses find() here, not each()
query.find(function(subscriptions) {
    _.each(subscriptions, function(subscription) { // or a for loop, if you don't use underscore

        // modify each subscription, then
        savePromises.push(subscription.save());

        // prepare each email then
        var emailPromise = Mailgun.sendEmail({ /* your email params object here */ });

        emailPromises.push(emailPromise);
    });
    // now do the saves
    return Parse.Promise.when(savePromises);
}).then(function() {
    // now do the emails
    return Parse.Promise.when(emailPromises);
}).then(function() {
    // Set the job's success status
    status.success("Subscriptions successfully fetched");

// and so on with your code

You might also consider combining the saves and the emails into one big array of promises, but it might be better to do it in two batches serially since they have different failure modes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!