关于云代码的承诺 [英] Promises on cloud code
问题描述
我正在编写一个云代码函数,用于在删除对象后运行许多任务. 我该如何干净地做而不嵌套?
I'm writing a cloud code function for run many task after delete an object. How can I do this cleanly and without nesting?
您可以并行运行任务,还是最好让它们顺序运行?如果可以,怎么办?
You can run tasks in parallel or it is better to have them sequentially? If so, how?
这是我的代码,您能帮我清理/更改吗?
This is my code, can you help me to clean/change?
Parse.Cloud.afterDelete("Photo", function(request) {
Parse.Cloud.useMasterKey();
//delete all related activities
var queryAct = new Parse.Query("Activity");
queryAct.equalTo("photo",{
__type: "Pointer",
className: "Photo",
objectId: request.object.id
});
queryAct.limit(1000);
queryAct.find({
success: function(activities) {
Parse.Object.destroyAll(activities, {
success: function() {
//delete all related hashtags
var queryTags = new Parse.Query("hashTags");
queryTags.equalTo("photo",{
__type: "Pointer",
className: "Photo",
objectId: request.object.id
});
queryTags.limit(1000);
queryTags.find({
success: function(hashtags) {
Parse.Object.destroyAll(hashtags, {
success: function() {},
error: function(error) {
console.error("Error deleting related hashtags " + error.code + ": " + error.message);
}
});
},
error: function(error){
console.error("Error finding related hashtags " + error.code + ": " + error.message);
}
});
},
error: function(error) {
console.error("Error deleting related activities " + error.code + ": " + error.message);
}
});
},
error: function(error) {
console.error("Error finding related activities " + error.code + ": " + error.message);
}
});
});
谢谢大家!
推荐答案
在这里find.then.delete是一系列连续的任务.但是,您可以并行删除hastags
和activities
,因为它们似乎并不相关.请查看承诺指南以获取更多信息.
您的代码可以缩短,如下所示:
Here find.then.delete is a series of tasks in a row. However, you can delete your hastags
and activities
in parallel since they don't seem to be dependent. Please check promises guide for more info.
Your code can be shortened as below:
var Photo = Parse.Object.extend("Photo");
Parse.Cloud.afterDelete("Photo", function(request) {
Parse.Cloud.useMasterKey();
var photo = Photo.createWithoutData(request.object.id);
var queryAct = new Parse.Query("Activity");
queryAct.equalTo("photo", photo);
queryAct.limit(1000);
var promiseForActivities = queryAct.find().then(function (activities) {
return Parse.Object.destroyAll(activities);
});
var queryTags = new Parse.Query("hashTags");
queryTags.equalTo("photo", photo);
queryTags.limit(1000);
var promiseForHashtags = queryTags.find().then(function (hashtags) {
return Parse.Object.destroyAll(hashtags);
});
return Parse.Promise.when([promiseForActivities, promiseForHashtags]).then(function () {
console.log("Done");
}, function (err) {
console.error(err);
});
});
请注意,您无需创建自己的指针对象,例如:
Note that you don't need to create your own pointer objects like:
{
__type: "Pointer",
className: "Photo",
objectId: "someId"
}
相反,您可以使用 createWithoutData 的快捷方式:
Rather you can use createWithoutData which is a shortcut for:
var Photo = Parse.Object.extend("Photo");
var p = new Photo();
p.id = "someId";
我还认为直接通过request.object
就足够了,就像queryAct.equalTo("photo", request.object);
I also think that it could be enough to pass the request.object
directly, like queryAct.equalTo("photo", request.object);
这篇关于关于云代码的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!