Parse platform 云代码中的一系列httprequest
我想在云代码中执行一系列httprequest。这样做很好。Parse platform 云代码中的一系列httprequest,parse-platform,Parse Platform,我想在云代码中执行一系列httprequest。这样做很好。 我的URL是这样排列的: Parse.Cloud.define("serialRequests", function(request, response) { var urls = ["http://www.htmldog.com/examples/headings1.html", "http://www.htmldog.com/examples/headings2.html", "http://www.htmldog.com/e
我的URL是这样排列的:
Parse.Cloud.define("serialRequests", function(request, response) {
var urls = ["http://www.htmldog.com/examples/headings1.html", "http://www.htmldog.com/examples/headings2.html", "http://www.htmldog.com/examples/lists1.html", "http://www.htmldog.com/examples/lists2.html"];
var promise = Parse.Promise.as();
var count = 0;
for (var i = 0; i < urls.length; i++) {
var url = urls[i];
promise = promise.always(function() {
return Parse.Cloud.httpRequest({
url: url,
success: function(httpResponse) {
console.log("Response: " + httpResponse.text);
count++;
},
error: function(httpResponse) {
console.log("Status: " + httpResponse.status);
count++;
}
});
});
}
promise.always(function() {
console.log("Count: " + count);
return response.success();
});
});
我做错了什么?除了我的解决方案,还有其他选择吗?我来晚了,但我猜你是在用你所说的方式凌驾于承诺之上,而不仅仅是延伸链条 这些需要一个接一个地运行吗?如果没有,则应该为每次迭代创建一个新的Parse.Promise(),并将其添加到数组中。然后,在for循环之后,返回Parse.Promise.when(arrayOfPromsies).Then(),其中Then()包含最后一个Promise调用,该调用将返回计数和所有这些。这要求调用httprequest的顺序实际上并不重要,只是它们都会发生,不管有多少失败,在我看来,这可能是您想要的
如果它们确实需要一个接一个地运行,则必须将实现更改为使用helper函数,在该函数中,传递数组的计数、当前索引和对数组的引用,并返回对函数本身的调用,增加索引,使用.then()从函数返回承诺。再次查看系列示例中的承诺。它不总是使用
。另外,你能把运行时打印的内容的完整日志包括在内吗?@HectorRamos我已将完整日志添加到问题中<代码>始终用于代替,然后
在一个请求失败时不中断链。如果使用,则使用,并且所有请求都成功,则日志相同。
I2014-07-19T06:11:41.807Z]Response: Response of the first URL in the array
I2014-07-19T06:11:42.084Z]Response: Response of the last URL in the array
I2014-07-19T06:11:42.356Z]Response: Response of the last URL in the array
I2014-07-19T06:11:42.624Z]Response: Response of the last URL in the array
I2014-07-19T06:11:42.640Z]Count: 4