【发布时间】:2014-03-06 21:13:19
【问题描述】:
我已经根据 Parse.com 中提供的 Parse 示例编写了代码来执行 Series 中的 Promise。
但是,顺序处理似乎无法正常工作。
以下代码多次调用名为“sampleCloudFuction”的云函数,但通过一系列承诺按顺序调用。
执行完循环后,app会调用另一个js函数来加载剩余的item(不包括处理过的item)。
这是多次调用云函数的循环:
var seriesPromise = new Parse.Promise.as();
$.each(items, function (i) {
..
..
count++;
if (count >= 25 || (i + 1) >= selectedItemsLength) {
.. ..
//Series Promise: The code to be executed in sequence being placed within the
//then() the existing promise
seriesPromise = seriesPromise.then(function () {
Parse.Cloud.run('sampleCloudFuction', itemsArray, function () {
console.log("success callback in JS");
var tempPromise = Parse.Promise.as();
return tempPromise;
}, function (error) {
alert("Error " + error.code + "::");
console.log("error callback in JS")
});
});
count = 0;
}
});
..
..
seriesPromise.then(function () {
//Fetch the approval state of the disabled button
alert("load remaining items");
});
以下函数将在执行循环后调用。但是,在接收所有早期请求的回调之前调用它。
seriesPromise.then(function () {
//Fetch the approval state of the disabled button
alert("load remaining items");
});
【问题讨论】:
-
您需要显示定义 seriesPromise 的代码
-
@wayne,我在上面的代码中加入了注释。 -----var seriesPromise = new Parse.Promise.as();--
标签: javascript parse-platform promise