簡體   English   中英

在已解決的承諾中使用承諾

[英]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.allprojetsPromises它在它之前的值。 您需要上一個$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM