等到嵌套的诺言解决 [英] Wait until nested promises resolve

查看:68
本文介绍了等到嵌套的诺言解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对数据库进行一堆嵌套调用,并希望在结果全部恢复后做一些事情。我是新来的诺言,如果我完全做错了(可能是这样),请原谅我。

I am trying to make a bunch of nested calls to the database and want to do something once the results have all come back. I'm new to promises so forgive me if I'm totally doing this wrong (probably the case)

我的代码当前是这样的:

My code currently is something like this:

getVideos(user).then((videos) => {
  videos.forEach((video) => {
    getImage(video).then(() => {
      getMembers().then((members) => {
        getComments().then((comments) => {
          getKeywords().then((keywords) => {
            getTranscript().then((transcript) => {
              console.log(members, comments, keywords, transcript)
            }
          }
        }
      }
    })
  })
})

这显然效率极低,因为 getMembers() getComments() getKeywords() getTranscript()不需要彼此等待,可以全部也可以异步调用 getImage()

This is obviously super inefficient because getMembers(), getComments(), getKeywords(), and getTranscript() dont need to wait for each other and can all be called asynchronously. Also getImage() can be called asynchronously for every video in the loop without waiting.

我正在尝试更改代码以将这些承诺捆绑成一个大的承诺,因此一旦解决,我就可以访问所有提取的数据在一个地方。这是我的尝试,但会打印

I'm trying to change the code to bundle up these promises into one big one so once it resolves I can access all the fetched data in one place. This is my attempt but it prints


Promise {...},Promise {...},Promise {...}, Promise {...}

Promise{...}, Promise{...}, Promise{...}, Promise{...}

代替获取的数据。

知道我在做什么错以及如何正确地将所有这些嵌套的诺言变成一个吗?谢谢

Any idea what I'm doing wrong and how I can properly change all these nested promises into one? Thanks

let members, comments, keywords, transcript

getVideos(user).then((videos) => {
   let promises = []
   videos.forEach((video) => {
      let p = getImage(video).then(() => {
        members = getMembers()
        comments = getComments()
        keywords = getKeywords()
        transcript = getTranscript()
        return Promise.all([members, comments, keywords, transcript])
      })
      promises.push(p)
  })
  return Promise.all(promises)
})
.then(() => {
  console.log(members, comments, keywords, transcript)
})


推荐答案

尝试使用两个 Promise.all s-每个个视频,一个用于成员评论关键字脚本

Try using two Promise.alls instead - one for each video, and one for the members, comments, keywords, and transcript:

getVideos(user).then((videos) => Promise.all(
  videos.map(video => getImage(video)
    .then(videoResp => Promise.all([
      getMembers(), // do you need to call these functions with `videoResp`?
      getComments(),
      getKeywords(),
      getTranscript(),
    ]))
  )
));

然后,promise链将以类似的方式解决

The promise chain will then resolve with something like

[ [
    // video 1
    v1members,
    v1comments,
    v1keywords,
    v1transcript,
  ],
  [
    // video 2
    v2members,
    v2comments,
    v2keywords,
    v2transcript,
  ],
  // ...
]

这篇关于等到嵌套的诺言解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆