angularjs $ q.all用于嵌套承诺

时间:2017-07-17 20:45:57

标签: angularjs angular-promise

尝试在AngularJS(1.6)中获取嵌套的异步REST请求,然后在完成所有请求后执行代码。

我尝试使用$q.all,但似乎这不会考虑内部请求(插曲集)。

如何修改以下示例,以便在完成所有请求后执行代码?

// foreach series --> load series details & seasons, foreach season --> load episodes
// when all requests are completed: do something

var requests = []

// e.g. series = [1]
series.forEach(function(seriesId) {
    requests.push(loadSeriesDetails(seriesId).then(function(data) {
        console.log("received details for series "+seriesId)
        // e.g. {id:1, title:"show 1"}
    }))
    requests.push(loadSeasons(seriesId).then(function (data) {
        console.log("received seasons for series "+seriesId+": ", data.seasons.length)
        // e.g. [{id:11, title:"season 1"}, {id:12, title:"season 2"}]
        data.seasons.map(function(e) {return e.id}).forEach(function(seasonId) {
            requests.push(loadEpisodes(seasonId).then(function (data) {
                console.log("received episodes for season "+seasonId+": ", data.episodes.length)
                // e.g.
                // season 11: [{id:111, title:"episode 1-1-1"}, {id:112, title:"episode 1-1-2"}, {id:113, title:"episode 1-1-3"}]
                // season 12: [{id:121, title:"episode 1-2-1"}, {id:122, title:"episode 1-2-2"}]
            }))
        })
    }))
})

$q.all(requests).then(function(result) {
    console.log("*** all requests completed ***")
    console.log(result.length)
})

上面的示例将返回2(1x loadSeriesDetails,1x loadSeasons)而不是4(1x loadSeriesDetails,1x loadSeasons,2x loadEpisodes)。

连连呢?提前谢谢!

更新:每个请求函数都是:

loadSeriesDetails = function(id) {
  url = "..."+id

  return $http.get(url).then(
    function (result) {
      return result.data
    }, function (error) {
      ̶r̶e̶t̶u̶r̶n̶ ̶e̶r̶r̶o̶r̶ 
      throw error;
  });
}

2 个答案:

答案 0 :(得分:2)

requests数组传递给$q.all时,应该具有需要解决的所有承诺。目前,您需要在之前的承诺解决后将loadEpisodes的承诺添加到requests

你可以解决这个问题,以便内部部分返回自己的$.q.all,它将被链接:

    var requests = [];

    series.forEach(function (seriesId) {
        requests.push(loadSeriesDetails(seriesId).then(function (data) {
            console.log("received details for series " + seriesId);
            return data;
        }));

        requests.push(loadSeasons(seriesId).then(function (data) {
            console.log("received seasons for series " + seriesId + ": ", data.seasons.length);
            return loadAllEpisodes(data.seasons);
        }));
    });

    function loadAllEpisodes(seasons) {
        var requests = seasons.map(function (season) {
            return loadEpisodes(season.id).then(function (data) {
                console.log("received episodes for season " + season.id + ": ", data.episodes.length);
                return data;
            });
        });

        return $q.all(requests);
    }

    q.all(requests).then(function(result) {
        console.log("*** all requests completed ***");
        console.log(result.length);
    });

请注意,在您的示例中,您仍然只会在外部$q.all返回的最终数组中看到2个结果,因为您从每个链的最后一个承诺中获得结果。因此,您可能需要调整返回语句,以便按照您需要的方式对信息进行分组。

答案 1 :(得分:2)

Frank's answer的变体,与原始代码段保持一致。

messages_all