在自身内部反应本机异步功能

时间:2018-12-07 12:17:00

标签: javascript react-native asynchronous promise axios

我正在尝试创建类似于注释线程的东西,对于每个注释子对象,该函数使用自身通过axios获取内容,如果还有其他子对象,则重复该过程。

之外,其他所有功能似乎都可以使用
        fullComment.listComments = await this.handleKids(fullComment.kids)

由于某种原因该函数不等待,我如何使其等待?

async handleKids(kids) {
      const listComments = [], promises = []
      kids.forEach(async (kid) => {
        promises.push(axios.get(url + kid + `.json`))
      })
      Promise.all(promises).then(async (promise) => {
        promise.forEach(async (resp) => {
          let fullComment = resp.data;
          if (fullComment.kids) {
            // How can I make this function to wait for this?
            fullComment.listComments = await this.handleKids(fullComment.kids)
          }
          listComments.push(fullComment)
          return listComments
        })
      })
    }

编辑:根据要求,我将forEach更改为:

      await Promise.all(promises).then(async (list) => {
        for await (var resp of list) {
          let fullComment = resp.data;
          if (fullComment.kids) {
            fullComment.listComments = await this.handleKids(fullComment.kids, true)
          }
          listComments.push(fullComment)
          return listComments
        }
      })

但这没用

0 个答案:

没有答案