代码之家  ›  专栏  ›  技术社区  ›  wivku

angularjs$q。全部用于嵌套承诺

  •  1
  • wivku  · 技术社区  · 7 年前

    $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 loadSeasons )而不是4(1x LoadSeries详细信息 ,1x 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 回复  |  直到 7 年前
        1
  •  3
  •   wivku    7 年前

    Frank's answer

    // 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"}]
            var innerRequests = [] // <<<==============
            data.seasons.map(function(e) {return e.id}).forEach(function(seasonId) {
                innerRequests.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"}]
                }))
            })
            return $q.all(innerRequests) //  <<<==============
        }))
    })
    
    $q.all(requests).then(function(result) {
        console.log("*** all requests completed ***")
        console.log(result.length)
    })
    
        2
  •  2
  •   georgeawg    7 年前

    你的 requests 数组应该具有在将其传递给时需要解决的所有承诺 $q.all .目前,您正在添加您的承诺 loadEpisodes 请求

    您可以将其分解,以便内部部分返回自己的部分 $.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 ,因为你正在从每条链的最后承诺中获得结果。因此,您可能需要调整返回语句,使其按您需要的方式对信息进行分组。