使用Promise和解析Javascript代码避免异步任务
我对解析javascript代码非常陌生,我对异步任务有一个问题。 所以我认为我必须用承诺 因此,我的代码的目标是获取创建体育赛事的用户,并在我获得他们后向他们发送通知。 由于异步任务,在使用Parse query.find方法获取用户id之前,将到达发送通知的代码 这是我的密码:使用Promise和解析Javascript代码避免异步任务,javascript,parse-platform,promise,Javascript,Parse Platform,Promise,我对解析javascript代码非常陌生,我对异步任务有一个问题。 所以我认为我必须用承诺 因此,我的代码的目标是获取创建体育赛事的用户,并在我获得他们后向他们发送通知。 由于异步任务,在使用Parse query.find方法获取用户id之前,将到达发送通知的代码 这是我的密码: //Background job whos goal is to send a push to the creator and the participants of // a sport event Parse
//Background job whos goal is to send a push to the creator and the participants of
// a sport event
Parse.Cloud.job("sendPushOneHourBefore", function(request, response) {
var query = new Parse.Query("SportEvent");
var now = new Date();
now.setHours(now.getHours()+1);
query.greaterThan("date", now);
now.setMinutes(now.getMinutes()+30);
query.lessThan("date", now);
query.include("_User");
var userIds = [];
//Request to find all the sport events within an hour and a half
return query.find().then(function(sportevents) {
//getting the ids of the sportevent user's
for (var i = 0; i < sportevents.length; ++i) {
var userRelation = sportevents[i].relation("user");
userRelation.query().find().then(function(users){
{ for (var j in users)
{
console.log(users[j].id);
userIds[i] = users[j].id;
console.log("userIds[i]"+userIds[i]);
console.log("i:"+i);
console.log("userIds[0]"+userIds[0]);
console.log("userIds[1]"+userIds[1]);
}
}
}
);
// );
}
return Parse.Promise.as("Hello");
}.then(function(hello){
console.error("Error finding sport events " + error.code + ": " + error.message);
console.log("userIds[1] dans find-"+userIds[1]+"-");
var queryPush = new Parse.Query(Parse.Installation);
queryPush.containedIn('userId', userIds);
Parse.Push.send({
where: queryPush,
data: {
alert: "Votre evenement sportif va commencer bientot! Verifiez l'heure sur Whatteam!"
}
}, {
success: function() {
// Push was successful
},
error: function(error) {
// Handle error
}
});
return Parse.Promise.as("Hello");
}),function(error) {
}
);
});
非常感谢任何能帮助我的人
Sebastien要将承诺连成一系列,您可以编写以下内容:
...
//Request to find all the sport events within an hour and a half
return query.find().then(function(sportevents) {
var promise = Parse.Proimse.as();
//getting the ids of the sportevent user's
for (var i = 0; i < sportevents.length; ++i) {
var relationQuery = sportevents[i].relation("user").query();
promise = promise.then(function(){
return relationQuery.find().then(function(users) {
for (var j in users) {
console.log(users[j].id);
userIds[i] = users[j].id;
console.log("userIds[i]"+userIds[i]);
console.log("i:"+i);
console.log("userIds[0]"+userIds[0]);
console.log("userIds[1]"+userIds[1]);
}
return Parse.Promise.as('Hello')
});
});
}
return promise
}).then(...)
。。。
//请求在一个半小时内查找所有体育赛事
返回query.find().then(函数(sportevents){
var promise=Parse.Proimse.as();
//获取sportevent用户的ID
对于(变量i=0;i
还可以查看文档中的部分。您刚刚错过了“'”之后的“}”,非常感谢您的回答!你是莱特。我还必须删除console.error才能运行它。但是我不明白为什么在“userRelation.query().find().then(function(users)”之后不调用“then(function(hello)”。你对这个问题有什么想法吗?再次感谢。你的回答很好!我刚刚将“Parse.Proimse.as();”改为“Parse.Promise.as();”并删除了r“eturn Parse.Promise.as('hello')”,还添加了“;”在“回报承诺”的末尾。
...
//Request to find all the sport events within an hour and a half
return query.find().then(function(sportevents) {
var promise = Parse.Proimse.as();
//getting the ids of the sportevent user's
for (var i = 0; i < sportevents.length; ++i) {
var relationQuery = sportevents[i].relation("user").query();
promise = promise.then(function(){
return relationQuery.find().then(function(users) {
for (var j in users) {
console.log(users[j].id);
userIds[i] = users[j].id;
console.log("userIds[i]"+userIds[i]);
console.log("i:"+i);
console.log("userIds[0]"+userIds[0]);
console.log("userIds[1]"+userIds[1]);
}
return Parse.Promise.as('Hello')
});
});
}
return promise
}).then(...)