如何让 promise.all 等待嵌套的 promise.all?

How to make promise.all wait for nested promise.all?

所以我对 JavaScript Promise 有疑问。为了减少依赖性,我正在使用本机实现。

我需要的示例。

我需要检索书籍、书籍作者和购买的列表。 我还需要每个作者的作者简介。完成所有这些后,我需要创建一组不错的作者,其中包含他们的书籍和每本书的购买清单。

列表和配置文件是单独的 API JSON 调用。唯一的依赖是我需要一个作者列表才能获得作者简介。

我已经用 Promises 解决了这个问题。 我使用 Promise.all 获得 3 API JSON 请求:作者、书籍和购买。 我使用另一个 Promise.all 来获取每个作者的所有配置文件(我遍历列表,为每个配置文件映射 url 并并行发送一批请求)。

我一拿到作者列表就运行配置文件请求批处理,因此在作者列表承诺的"Then"处理程序中。

现在,问题:

为确保所有承诺、3 个列表和所有配置文件都将在我组装库集之前完成,我需要在完成所有列表后发送一批配置文件请求,在第一个 Promise.all 然后是处理程序。

但是:购买书籍列表比作者列表花费的时间长得多,我不想等待所有这些发送一批配置文件请求,所以我在 Then 处理程序中发送它作者列表承诺,一旦我获得信息,这些就会开始。

但是,嵌套的 Promise.all 不计入其父级 Promise.all Then 处理程序,因此由于我的 FinalFunction 位于顶层 Promise.all 的 Then 中,它可能(并且有时会)在嵌套 Promise.all 完成检索所有作者个人资料之前触发。

我需要能够尽快启动所有 Promise 请求,但只有一批作者请求取决于一个 promise 是否已完成才能启动,所以我需要等待那个。其他的都要独立开始。

伪代码

Promise.all(
  requestBooks().then(){},
  requestAuthors().then(){
    GenerateArrayOfAuthorUris();
    // now send a promisifyed batch of per-author requests
    Promise.all(
      [array of author uris to run requests for]
    )
    .then(){
      // If I run this here, I may not have upper-level requests done
      runCalculationsPerAuthorForAllAuthorsBooksPurchasesReceived();
    }
  },
  requestPurchases().then(){},
)
.then(){
  // this will fire when 3 top requests are done, but won't wait for 
  //   the nested Promise.all with per-author requests
  runCalculationsPerAuthorForAllAuthorsBooksPurchasesReceived();
}

如果我这样做,我会浪费宝贵的时间等待请求,我不需要等待只是为了开始每个作者的请求:

Promise.all(
  requestBooks().then(){},
  requestAuthors().then(){
    GenerateArrayOfAuthorUris();
  },
  requestPurchases().then(){},
)
.then(){
    // now send a promisifyed batch of per-author requests
    Promise.all(
      [array of author uris to run requests for]
    )
    .then(){
      // If I run this here, I may not have upper-level requests done
      runCalculationsPerAuthorForAllAuthorsBooksPurchasesReceived();
    }
}

希望这能阐明我的需要。

谢谢。

这是代码示例:https://jsbin.com/qizohasofa/edit?js,console

正如评论中告诉您的那样,您没有return 函数中的任何内容,因此then 不知道要等待什么内部承诺。

function getJokeCategories() {
    return Promise.all([
//  ^^^^^^
        pgetJSON("http://api.icndb.com/categories"),
        pgetJSON("http://api.icndb.com/jokes/count").then(function(data) {
            var jokesToGet = [];
            for (var i=0; i<data; i++){
                jokesToGet.push("http://api.icndb.com/jokes/"+i);
            }
            return Promise.all(jokesToGet.map(function(jk) {
//          ^^^^^^
                return pgetJSON(jk).then(function(joke) {
//              ^^^^^^
                    console.log(jk + " just returned", joke);
                    return joke;
//                  ^^^^^^
                });
            })).then(function(jokes) {
                console.log("All jokes returned. This does happen only when all jokes are retrieved.");
                return {count:data, jokes:jokes};
//              ^^^^^^
            });
        })
    ]);
}
getJokeCategories().then(function(result) {
    console.log(result, "This does happen at the very end when joke count, joke categories and all jokes are returned.");
});