如何在for循环中组织承诺,并在javascript中的函数之间传递参数?
我正试图用javascript实现我的第一个承诺。我有这些功能。必须完成每项功能才能进入下一步:如何在for循环中组织承诺,并在javascript中的函数之间传递参数?,javascript,promise,Javascript,Promise,我正试图用javascript实现我的第一个承诺。我有这些功能。必须完成每项功能才能进入下一步: F1 --> get Current User Id and save it to var "MyUserID" F2 --> getUserEvent (example 10 events) Go to a for loop for each event and for each iteration save value to var "Ev
F1 --> get Current User Id
and save it to var "MyUserID"
F2 --> getUserEvent (example 10 events)
Go to a for loop for each event
and for each iteration save value to var "EventId"
F3--> Get info for that specific event from facebook
This function has 2 parameters (MyUserID,EventId)
F4--> Get image for that specific event
This function has 1 parameter (EventId)
f5--> Save info to DB
Go back to the loop
为了实现这一点,我做了以下工作,但我知道它不起作用。如果任何有承诺经验的人能给我展示一下原型,我真的很感激。我不确定如何在函数之间传递参数:
var getMyID = function() {
return new Promise(function(resolve, reject) {
// Get my id from facebook
FB.api('/me', function(response) {
if (response && !response.error) {
console.log("response== "+ JSON.stringify(response));
retuen(response.id);//MyUserId
}
});
});
};
getMyID.then(function(myID) {
FB.api('/me/events', function(response) {
console.log("response== "+ JSON.stringify(response));
for(i=0; i<response.data.length;i++) {
console.log("eventID= "+response.data[i].id);
getEvent(response.data[i].id,myID);
}
});
}).catch(function(error) {
console.log('oh no', error);
}).then(function(eventID,myID) {
FB.api("/"+ eventID , function (response3) {
if (response3 && !response3.error) {
//....
if(myID == response3.owner.id && diff < 0 )
{
//save to DB -- Another place I need to add promise
}
}
});
});
var getMyID=function(){
返回新承诺(功能(解决、拒绝){
//从facebook获取我的id
FB.api('/me',函数(响应){
if(response&&!response.error){
log(“response==”+JSON.stringify(response));
retuen(response.id);//MyUserId
}
});
});
};
getMyID.then(函数(myID){
FB.api('/me/events',函数(响应){
log(“response==”+JSON.stringify(response));
对于(i=0;i我看到以下错误:
getMyID
返回的承诺永远不会得到解决或拒绝
getMyID。那么
就不起作用了,因为getMyID
是一个函数
,而不是承诺
。也许你的意思是getMyID()。然后(…)
- 调用
/me/events
的回调应该返回一个承诺
,以便它是异步的。正如当前一样,它将启动对Facebook API的调用,并立即转到下一个然后处理程序
- 与最后的
和处理程序相同
- 您应该将
catch
放在末尾,以便它捕获所有错误
- 键入您的
retuen
而不是return
为了安全起见,我建议您在Facebook API周围创建一个承诺返回包装器。类似如下:
function fbAPIRequest(url) {
return new Promise(function (resolve, reject) {
FB.api(url, function(response) {
if (response && !response.error) {
resolve(response);
} else {
reject(response);
}
});
};
}
这将使您的代码更简单:
var myUserID;
fbAPIRequest('/me')
.then(function (response) {
myUserID = response.id;
return fbAPIRequest('/me/events');
})
.then(function (response) {
var eventPromises = response.data.map(function (data) {
return getEvent(data.id, myUserID);
});
return Promise.all(eventPromises);
})
.catch(function(error) {
console.log('oh no', error);
});
function getEvent(eventID, userID) {
return fbAPIRequest('/' + eventID)
.then(function (response) {
if (response.owner.id === userID && diff < 0) {
return saveToDB(response);
}
});
}
function saveToDB(event) {
return new Promise(...);
}
var-myUserID;
fbAPIRequest(“/me”)
.然后(功能(响应){
myUserID=response.id;
返回fbAPIRequest('/me/events');
})
.然后(功能(响应){
var eventPromises=response.data.map(函数(数据){
返回getEvent(data.id,myUserID);
});
返回承诺。所有(事件承诺);
})
.catch(函数(错误){
console.log('oh no',错误);
});
函数getEvent(eventID、userID){
返回fbAPIRequest('/'+eventID)
.然后(功能(响应){
if(response.owner.id==userID&&diff<0){
返回saveToDB(响应);
}
});
}
函数saveToDB(事件){
返回新承诺(…);
}
您会注意到,我通过返回值(这将成为链中下一个回调使用的值)、返回将解析为值的承诺或利用闭包(如设置myUserID
变量)在回调之间传递数据.谢谢你的回答。我不知道你为什么使用response.data.map?我需要一个foor循环!我假设你想为数组中的每个事件执行一个异步任务。所以我使用map
将数组转换为Promise
对象的数组,并使用Promise.all
并行执行它们。你为什么需要循环?