如何使response.success()等?

时间:2015-03-23 04:59:19

标签: javascript parse-platform promise cloud-code

在下面的Cloud函数中,我需要知道在何处以及如何放置response.success(),以便在完成所有工作后执行,而不是之前执行。 我已经检查过该函数正在按预期执行其工作。唯一的问题是我提到的问题。 我正在尝试使用Promises,但到目前为止我所做的并没有奏效。

 Parse.Cloud.define
 ("deleteUnitAndDependencies", function(request, response) {
    var unitListQuery;
    unitListQuery = new Parse.Query("UnitList");
    unitListQuery.equalTo("objectId", request.params.unitID);
    unitListQuery.equalTo("ownerID", request.params.userID);
    unitListQuery.find().then(function(resUnit) {
    var sentenceListQuery,sentenceListStatus;
    sentenceListQuery = new Parse.Query("SentenceList");
    sentenceListQuery.equalTo("unit", resUnit[0]);
    sentenceListStatus = sentenceListQuery.find();
    resUnit[0].destroy({});
    return sentenceListStatus;
    }).then(function(resSentence) {
    var translatListQuery,translatListQStatus;
    translatListQuery = new Parse.Query("TranslatList");
    for (iS = 0; iS < resSentence.length; iS++) {
        if (iS == 0 ) {
            translatListQuery.equalTo("sentence", resSentence[iS]);
            continue;
        }
        translatListQuery = Parse.Query.or(translatListQuery,
                                           (new Parse.Query("TranslatList")).equalTo
                                           ("sentence", resSentence[iS]));
    }
    translatListQStatus = translatListQuery.find();
    for (iS = 0; iS < resSentence.length; iS++) {
        resSentence[iS].destroy({});
    }
    return translatListQStatus;
    }).then(function(resTranslat) {
    for (iT = 0; iT < resTranslat.length; iT++) {
        resTranslat[iT].destroy({});
    }
    });
   });

4 个答案:

答案 0 :(得分:1)

将一个函数放入doSomeBigWork,当它完成异步时,让它调用该函数。

doSomeBigWork(function() {
    response.success();
});

答案 1 :(得分:1)

您的代码中有多个部分可以返回promise。您需要将这些包含在您的承诺链中。这就像使用return语句一样简单。

此块例如:

    translatListQStatus = translatListQuery.find();
    for (iS = 0; iS < resSentence.length; iS++) {
        resSentence[iS].destroy({});
    }
    return translatListQStatus;
}).then(function(resTranslat) {
    for (iT = 0; iT < resTranslat.length; iT++) {
        resTranslat[iT].destroy({});
    }
});

您希望等待destroy次调用完成。请查看Promise Chaining in Parallel

上的帮助的此部分
    var findResults;
    translatListQStatus = translatListQuery.find()
    .then(function(resTranslat) {
        var destroyPromises = [];
        for (iS = 0; iS < resSentence.length; iS++) {
            destroyPromises.push(resSentence[iS].destroy({}));
        }

        // save these so we can return them at the end
        findResults = resTranslat;

        // return a promise here
        return Parse.Promise.when(destroyPromises);
    })
    .then(function() {
        // return the results we saved
        return findResults;
    });

    return translatListQStatus;
}).then(function(resTranslat) {
    var destroyPromises = [];
    for (iT = 0; iT < resTranslat.length; iT++) {
        destroyPromises.push(resTranslat[iT].destroy({}));
    }

    return Parse.Promise.when(destroyPromises);
});

答案 2 :(得分:1)

米歇尔,祝贺它成功。对于那些仅使用承诺仅仅几天的人来说,这是一项不小的成就。

你会发现通过创建一个destroy()实用程序,你可以避免重复代码并压缩主程序。

通过该实用程序和进一步的简化/重新安排,我最终得到了这个:

Parse.Cloud.define("deleteUnitAndDependencies", function(request, response) {
    //A utility function that gets called twice
    function destroy(arr) {
        return Parse.Promise.when(arr.map(function(r) {
            return r.destroy({});
        }));
    }
    var unitListQuery = new Parse.Query("UnitList");
    unitListQuery.equalTo("objectId", request.params.unitID);
    unitListQuery.equalTo("ownerID", request.params.userID);
    unitListQuery.find().then(function(resUnit) {
        var sentenceListQuery = new Parse.Query("SentenceList");
        sentenceListQuery.equalTo("unit", resUnit[0]);
        return sentenceListQuery.find().then(function(resSentence) {
            var translatListQuery = new Parse.Query("TranslatList");
            translatListQuery.equalTo("sentence", resSentence[0]);
            for (var iS = 1; iS < resSentence.length; iS++) {//loop counter now starts at 1
                translatListQuery = Parse.Query.or(translatListQuery, (new Parse.Query("TranslatList")).equalTo("sentence", resSentence[iS]));
            }
            return translatListQuery.find().then(destroy).then(function() {
                return destroy(resSentence.concat(resUnit[0]));
            });
        });
    }).then(response.success);
});

未测试

修改

由于破坏似乎不需要彼此等待,你应该能够进行两次.concat()技巧,然后拨打destroy()一次。

return translatListQuery.find().then(function(resTranslat) {
    return destroy(resTranslat.concat(resSentence).concat(resUnit[0]));
});

答案 3 :(得分:0)

如果有人遇到类似问题,我会在此处提出我想出的解决方案。就我现在检查而言,它正在按照我想要的方式工作。 如果某位承诺专家对此有所了解并提出意见和建议,请告知我们。

Parse.Cloud.define
("deleteUnitAndDependencies", function(request, response) {
 var unitListQuery;
 unitListQuery = new Parse.Query("UnitList");
 unitListQuery.equalTo("objectId", request.params.unitID);
 unitListQuery.equalTo("ownerID", request.params.userID);

 unitListQuery.find().then
 (function(resUnit) {
  // If there is no UNIT we return an error.
  if (!resUnit.length) response.error("NO-UNIT");

  var sentenceListQuery;
  sentenceListQuery = new Parse.Query("SentenceList");
  sentenceListQuery.equalTo("unit", resUnit[0]);

  sentenceListQuery.find().then
  (function(resSentence) {
   var translatListQuery,tmpQuery;
   translatListQuery = new Parse.Query("TranslatList");

   for (iS = 0; iS < resSentence.length; iS++) {
    if (iS == 0 ) {
        translatListQuery.equalTo("sentence", resSentence[iS]);
        continue;
    }

    tmpQuery = new Parse.Query("TranslatList");
    tmpQuery.equalTo("sentence", resSentence[iS]);
    translatListQuery = Parse.Query.or(translatListQuery,tmpQuery);
   }

   translatListQuery.find().then
   (function(resTranslat) {
    var destroyPromises = [];
    for (iT = 0; iT < resTranslat.length; iT++) {
        destroyPromises.push(resTranslat[iT].destroy({}));
    }
    return Parse.Promise.when(destroyPromises);
    }).then
   (function() {
    var destroyPromises = [];
    for (iS = 0; iS < resSentence.length; iS++) {
        destroyPromises.push(resSentence[iS].destroy({}));
    }
    return Parse.Promise.when(destroyPromises);
    }).then
   (function() {
    return resUnit[0].destroy({});
    }).then(response.success);
   });
  });
 });