[英]How to block multiple asynchronous Parse cloud calls until done?
I'm trying to achieve something very much like the following within Javascript on a Parse.com Cloud function: 我正在尝试在Parse.com Cloud函数的Javascript中实现类似于以下内容的功能:
Parse.Cloud.define("GatherThings", function(req, res) {
var array_whatToGet = req.params.getThese;
var returnVal = {};
if(array_whatToGet.indexOf("Thing1") > -1) {
Parse.Cloud.run("GetThing1", {}, {
success: function(results) {
returnVal['Thing1'] = results;
} //ignoring error for now
}
}
if(array_whatToGet.indexOf("Thing2") > -1) {
Parse.Cloud.run("GetThing2", {}, {
success: function(results) {
returnVal['Thing2'] = results;
} //ignoring error for now
}
}
if(array_whatToGet.indexOf("Thing3") > -1) {
Parse.Cloud.run("GetThing3", {}, {
success: function(results) {
returnVal['Thing3'] = results;
} //ignoring error for now
}
}
res.success(retVal);
}
Possible syntax errors here aside, when I try to do something like this I can get every called cloud function to run, but because of the async quality of these cloud functions I always get to retVal before anything finishes. 除了可能的语法错误外,当我尝试执行类似的操作时,我可以运行每个被调用的云函数,但是由于这些云函数的异步质量,我总是在任何事情完成之前就进入retVal。
Can I somehow chain or block these in a way that they are not dependent on the other to run, but the final return is dependent on whether or not everything has finished? 我能否以某种方式将它们链接或阻止,使它们不依赖于另一个来运行,但最终的回报取决于一切是否完成?
This is just what promises are for. 这正是诺言的目的。 All of the Parse functions return promises, which can be given a method to run upon completion. 所有的Parse函数都返回诺言,可以为诺言提供一种在完成时运行的方法。 Parse.Promise.when()
takes an array of promises and is fulfilled when all of the passed promises are. Parse.Promise.when()
接受一个promise数组,并在所有传递的promise被满足时实现。 We'd use it this way: 我们将以这种方式使用它:
// underscore.js is a great general purpose utility, mostly self-explanatory
var _ = require('underscore');
Parse.Cloud.define("GatherThings", function(req, res) {
var things = ["Thing1", "Thing2", "Thing3"];
var array_whatToGet = req.params.getThese;
var promises = [];
_.each(things, function(thing) {
if (_.contains(array_whatToGet, thing)) {
promises.push(getThing(thing));
}
});
Parse.Promise.when(promises).then(function(result) {
res.success(result);
}, function(error) {
res.error(error);
});
});
The question doesn't mention what GetThing1, GetThing2 and GetThing3 are, but it implies that they are other cloud functions. 这个问题没有提到GetThing1,GetThing2和GetThing3是什么,但这意味着它们是其他云函数。 Calling Cloud.run from the cloud is at best unnecessary. 最好不要从云中调用Cloud.run。 Lets assume that the things specify some object to be queried and make a regular, non-cloud function that returns a promise to perform a query (which the Query methods all do). 让我们假设事物指定了要查询的对象,并创建了一个常规的非云函数,该函数返回一个执行查询的承诺(Query方法都可以这样做)。
function getThing(aThing) {
var query = new Parse.Query("MyClass");
query.equalTo("theThingProperty", aThing);
// query.first() returns a promise that is fulfilled when the query is complete
return query.first();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.