Javascript parse-Swift中的链接承诺
是否可以在parse promise函数内部循环?我已经创建了这个方法,但是不断得到一个编译器错误。基本上我想循环数组中的一组值 define(“getNews”),函数(请求、响应){Javascript parse-Swift中的链接承诺,javascript,parse-platform,parse-cloud-code,Javascript,Parse Platform,Parse Cloud Code,是否可以在parse promise函数内部循环?我已经创建了这个方法,但是不断得到一个编译器错误。基本上我想循环数组中的一组值 define(“getNews”),函数(请求、响应){ })) 你很接近。需要实现的是,http请求的解析函数可以成为收集的承诺(即Promise.when())的解析函数 该函数的var arg参数将返回与请求相对应的http响应数组(一个响应用于“a”请求,另一个响应用于“b”) 另一种可能更有效的方法是将承诺返回解析函数链接到每个http请求之外。读起来有点困
})) 你很接近。需要实现的是,http请求的解析函数可以成为收集的承诺(即
Promise.when()
)的解析函数
该函数的var arg参数将返回与请求相对应的http响应数组(一个响应用于“a”请求,另一个响应用于“b”)
另一种可能更有效的方法是将承诺返回解析函数链接到每个http请求之外。读起来有点困难,但是如果http结果所执行的操作是另一个异步操作,并且如果该函数依赖于其他系统资源(与httpRequest不同),那么您可以让更多的机器像这样并行工作
var promises = _.map(count, function(v) {
return Parse.Cloud.httpRequest({method: 'GET',
url: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
}).then(function(httpResponse) {
var promise = someNonHttp_PromiseReturningFunction(httpResponse);
return promise;
});
});
Parse.Promise.when(promises).then(function () {
// arguments here will be the resolutions of those
// someNonHttp_... function promises
Hey dude今天早上尝试了您的解决方案,但似乎在将变量作为httprequest的参数传递到url时出现了问题它会中断,因为它只在传递字符串时起作用,因为参数是否有任何方法将变量传递到此参数url参数必须是字符串,但这包括任何计算结果为字符串的表达式。所以如果你有
var foo=”http://www"; var bar=“.google.com/”
,您当然可以通过url:foo+bar
。记录您作为url传递的内容,以确保它是您认为的内容。我已经用我正在使用的新代码更新了我的问题。问题是我已经尝试了您的建议,但是我在httprequest l中将newsUr变量作为url放入其中,这是应用程序破坏我的地方。在发布的代码,你有什么错误吗?我想你是在寻找Promise.when(Promise).then(function(results){var count=results.join(“\n”);…}
@Bergi返回的JSON中有/所以我的应用程序失败了,我似乎无法摆脱它们。请给我们看看返回的JSON。@Bergi只是一个片段,因为它太长了哈哈{“results”:[“{\'name\”:“creativebloq\u top\”,“count\”:10,“frequency\”:“Manual
// because it provides lots of useful stuff...
var _ = require('underscore');
Parse.Cloud.define("getNews", function (request, response) {
var count = ["a", "b"];
var promises = _.map(count, function(v) {
return Parse.Cloud.httpRequest({method: 'GET',
url: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
});
});
Parse.Promise.when(promises).then(function () {
var httpResponses = _.toArray(arguments);
var result = _.map(httpResponses, function(httpResponse) {
// For now i'll return string but later will put the data i created
return "string that will someday be a function of httpResponse";
});
return result;
}).then(function (result) {
response.success(result);
}, function (error) {
response.error(error);
});
});
var promises = _.map(count, function(v) {
return Parse.Cloud.httpRequest({method: 'GET',
url: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
}).then(function(httpResponse) {
var promise = someNonHttp_PromiseReturningFunction(httpResponse);
return promise;
});
});
Parse.Promise.when(promises).then(function () {
// arguments here will be the resolutions of those
// someNonHttp_... function promises