Return Pase.Query.each() 中的承诺无效
Return a promise in Pase.Query.each() don't work
在云代码后台作业中,我有一种方法可以从 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 来检索有关某个特定用户的信息。
根据 specification of the "each()" function:
If the callback returns a promise, the iteration will not continue until that promise has been fulfilled.
我return一个承诺,会在APIreturn结果时实现。
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 ()" 函数不会等待承诺实现。这可能是什么问题?
对于初学者来说,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.");
});
@mido22对第一种方法完全正确。只是 return 承诺 return 由 httpRequest 编辑。重复一下,修正 his/her 修正一点:
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;
});
};
关于第二种方法,缺少的(至少)是来自内部承诺的 return 值。 Parse.Promise.when 使逻辑更清晰,imo,接受一系列承诺并在所有承诺都实现时实现...
// 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);
});
在云代码后台作业中,我有一种方法可以从 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 来检索有关某个特定用户的信息。 根据 specification of the "each()" function:
If the callback returns a promise, the iteration will not continue until that promise has been fulfilled.
我return一个承诺,会在APIreturn结果时实现。
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 ()" 函数不会等待承诺实现。这可能是什么问题?
对于初学者来说,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.");
});
@mido22对第一种方法完全正确。只是 return 承诺 return 由 httpRequest 编辑。重复一下,修正 his/her 修正一点:
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;
});
};
关于第二种方法,缺少的(至少)是来自内部承诺的 return 值。 Parse.Promise.when 使逻辑更清晰,imo,接受一系列承诺并在所有承诺都实现时实现...
// 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);
});