在云代码背景作业中,我有一种从API检索信息的方法:
var getUserPageView=function(userid){
var promise=new Parse.Promise();
Parse.Cloud.httpRequest({
url: 'http://apiip.ch/api.php?date='+
formatDate(new Date())
}).then(function(httpResponse) {
// success
var text=httpResponse.text;
var obj=JSON.parse(text);
promise.resolve(parseInt(obj[0][1]));
},function(httpResponse) {
// error
console.error('Request failed with response code ' + httpResponse.status);
promise.reject();
});
return promise;
};对于所有用户,我必须调用API来检索有关一个特定用户的信息。根据"each()“函数的规范
如果回调返回一个承诺,则在该承诺实现之前,迭代不会继续。
当API返回结果时,我将返回一个承诺。
query=new Parse.Query(Parse.User);
query.each(function(user){
var promise=Parse.Promise().as();
promise=promise.then(function(){
getUserPageView(234).then(function(result){
console.log("User page view:"+ result);
//process user with API information
promise.resolve("a");
},function(){
console.log("error");
});
});
console.log("user processed");
return promise;
}).then(function(){
status.success("Credti update successfull.");
}, function(error){
status.error("Uh oh, something went wrong.");
});问题是getUserPageView()从未被调用过。或者更准确地说,我认为"each ()“函数不会等待承诺实现。这里有什么问题吗?
发布于 2015-07-21 10:07:23
首先,Parse.Cloud.httpRequest已经做出了承诺,无需包装:
var getUserPageView=function(userid){ // not sure why userid is required, it is not used anywhere.
return Parse.Cloud.httpRequest({
url: 'http://apiip.ch/api.php?date='+ formatDate(new Date())
}).then(function(httpResponse) { // success
var text=httpResponse.text;
var obj=JSON.parse(text);
return parseInt(obj[0][1]);
},function(httpResponse) { // error
console.error('Request failed with response code ' + httpResponse.status);
throw httpResponse;
});
};至于query.each(),我不太确定,只是盲目地尝试一下:
query=new Parse.Query(Parse.User);
query.each(function(user){
return getUserPageView(234).then(function(result){
console.log("User page view:"+ result); //process user with API information
},function(){
console.log("error");
});
}).then(function(){
status.success("Credti update successfull.");
}, function(error){
status.error("Uh oh, something went wrong.");
});发布于 2015-07-21 14:53:52
@mido22 22对于第一个方法是完全正确的。只需返回httpRequest返回的承诺即可。重复一遍,修复他/她的错误:
var getUserPageView=function(userid){
return Parse.Cloud.httpRequest({
url: 'http://apiip.ch/api.php?date='+
formatDate(new Date())
}).then(function(httpResponse) {
// success
var text=httpResponse.text;
var obj=JSON.parse(text);
return parseInt(obj[0][1]);
},function(httpResponse) {
// error
console.error('Request failed with response code ' + httpResponse.status);
return httpResponse;
});
};对于第二种方法,所缺少的(至少)是来自内部承诺的返回值。Parse.Promise.when使逻辑变得更清楚了,当所有的承诺都实现时,海事组织做出了一系列的承诺并实现了承诺。
// very handy for iteration and much more
var _ = require('underscore');
query=new Parse.Query(Parse.User);
query.limit(4); // start out with a small number to make sure it works (all users will eventually run afoul of resource limits)
query.find().then(function(users){
var promises = _.map(users, function(user) {
return getUserPageView(234);
});
return Parse.Promise.when(promises);
}).then(function(){
status.success(_.toArray(arguments)); // when() returns an array of results for each input promise, these are in var args of the success function
}, function(error){
status.error(error);
});https://stackoverflow.com/questions/31534625
复制相似问题