【问题标题】:What type of Object needs to be returned into await for it not being undefined?什么类型的对象需要返回到等待中,因为它不是未定义的?
【发布时间】:2020-11-19 13:00:17
【问题描述】:

让我在这里尽可能清楚。

  1. 我调用 getVids 函数
  2. 它直接转到它的return await Promise.all...
  3. 然后它进入递归,singleChannelWithNewVids 对象被videos 数组转储
  4. 当没有更多res.data.nextPageToken 响应时,我将返回那个有点大的singleChannelWithNewVids 对象。
  5. 我的console.log(singleChannelWithNewVids, 'RETURING') 行记录此对象存在
  6. 但是当const gettingVids = await getVidsWithPage(e) 发生并且我记录console.log('gettingVids', gettingVids) 时,它会记录该对象未定义。

这是为什么呢?正如你所看到的,我已经尝试了所有的评论,并且每一个都返回 undefined 但记录了一个普通的对象......

const channelsWithPlaylistIds = [{
    channelName: 'TokyoStreetView - Japan The Beautiful',
    channelId: 'UCAxMEpfzdJ2dkrplSWehgcA',
    playlistIds: [ 'UUAxMEpfzdJ2dkrplSWehgcA' ],
    videos: []
}]

async getVids(channelsWithPlaylistIds) {
    const getVidsWithPage = async (e, pageToken) => {
        return axios(this.playlistItemsUrl(e.playlistIds[0], pageToken)).then(async res => {
            const singleChannelWithNewVids = { ...e, videos: [...e.videos, ...res.data.items]}

            if(res.data.nextPageToken) {
                console.log('RECUSING')
                await getVidsWithPage(singleChannelWithNewVids, res.data.nextPageToken)
            } else {
                console.log(singleChannelWithNewVids, 'RETURING')
                return Promise.resolve(singleChannelWithNewVids)
                // return new Promise(resolve => { resolve(singleChannelWithNewVids) })
                // return Promise.all([singleChannelWithNewVids])
                // return singleChannelWithNewVids
            }
        })
    }

    return await Promise.all(channelsWithPlaylistIds.map(async e => {
        const gettingVids = await getVidsWithPage(e)
        console.log('gettingVids', gettingVids)
        return gettingVids
    }))
}

是的,我不使用分号,现在是 2020 年,处理它

【问题讨论】:

  • 不使用分号与 2020 年没有任何关系。如果您使用代码 uglifier/minimizer(您应该将其用于生产代码),如果您不使用分号,您将面临引入逻辑问题的风险。
  • @Taplar 你读过这个问题吗?我知道我会遇到什么问题。这句话应该是个小笑话。请不要在他们没有问的事情上对别人说教,你应该更好地帮助他们解决他们需要帮助的事情......
  • 如果您想避免征求对声明的反馈,请考虑不包括该声明。
  • 您确定需要const gettingVids = await getVidsWithPage(e) 中的await 吗?
  • @haim770 我想是这样......它接收到一堆请求,这些请求取决于映射函数中特定数组的先前响应数据。至少我还没有为这个问题找到更优雅的解决方案。

标签: javascript ecmascript-6 async-await axios es6-promise


【解决方案1】:

你所拥有的是一个 递归 函数,除了最后一个之外,在所有情况下都不返回

为了理解这一点,想象一下第一次执行getVidsWithPage的线程。它进行axios 调用,看到if(res.data.nextPageToken) 为真,进入该if 语句,然后调用一个函数(在本例中为它本身,但这实际上并不重要),然后结束执行而不返回任何内容.

在递归期间,只有最后一次调用getVidsWithPage返回,但它返回到其中之一:

await getVidsWithPage(singleChannelWithNewVids, res.data.nextPageToken)

...结果被丢弃。

无论如何,只需在此处添加return

return await getVidsWithPage(singleChannelWithNewVids, res.data.nextPageToken)

顺便说一句,在看到你们的 cmets 之后,我想提出一个不相关的改进 - YSK,每个 函数自动标记为 async,总是返回一个 Promise。这很有用,这意味着您的Promise.resolve 是多余的,您永远不需要使用.then()。您可以重写您当前的代码以使其更加简洁,如下所示:

getVids(channelsWithPlaylistIds) {
    const getVidsWithPage = async (e, pageToken) => {
        let res = await axios(this.playlistItemsUrl(e.playlistIds[0], pageToken))
        const singleChannelWithNewVids = { ...e, videos: [...e.videos, ...res.data.items]}

        if(res.data.nextPageToken) {
            console.log('RECUSING')
            return getVidsWithPage(singleChannelWithNewVids, res.data.nextPageToken)
        } else {
            console.log(singleChannelWithNewVids, 'RETURING')
            return singleChannelWithNewVids
        }
    }

    return Promise.all(channelsWithPlaylistIds.map(getVidsWithPage))
}

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2021-07-17
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    相关资源
    最近更新 更多