[英]Use promise inside resolved promise
我試圖基於另一個數組的結果來獲取一個數組:
for (var i = 0; i < result.data.ftListes.length; i++) {
//Get the name of the task related to the timesheet
tachesPromises.push(Tache.getTachebyId(result.data.ftListes[i].tacheid));
// I tried to get the project here but it didn't work
}
//resolve promises
$q.all(tachesPromises).then(function(taches) {
taches.forEach(function(el) {
tasks.push(el.data.tachelistes);
projetsPromises.push(Projet.getProjetbyId(el.data.tachelistes.projet_id));
});
});
$q.all(projetsPromises).then(function(p) {
p.forEach(function(el) {
projet.push(el.data.projetsListe);
});
});
似乎我的get請求正在工作,但看不到結果
你調用$q.all
上projetsPromises
它在它之前的值。 您需要在上一個$q.all
調用的處理程序中執行此操作。
for (var i = 0; i < result.data.ftListes.length; i++) {
//Get the name of the task related to the timesheet
tachesPromises.push(Tache.getTachebyId(result.data.ftListes[i].tacheid));
// I tried to get the project here but it didn't work
}
//resolve promises
$q.all(tachesPromises).then(function(taches) {
taches.forEach(function(el) {
tasks.push(el.data.tachelistes);
projetsPromises.push(Projet.getProjetbyId(el.data.tachelistes.projet_id));
});
$q.all(projetsPromises).then(function(p) { // Moved
p.forEach(function(el) { //
projet.push(el.data.projetsListe); //
}); //
}); //
});
僅僅為了它的價值,一開始,您的for
循環就是Array#map
設計要做的:
tachesPromises = result.data.ftListes.map(function(e) {
return Tache.getTachebyId(e.tacheid);
});
或帶有ES2015 +箭頭功能:
tachesPromises = result.data.ftListes.map(e => Tache.getTachebyId(e.tacheid));
...當然,假設result.data.ftListes
是一個數組。 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.