【发布时间】:2020-11-19 13:00:17
【问题描述】:
让我在这里尽可能清楚。
- 我调用 getVids 函数
- 它直接转到它的
return await Promise.all... - 然后它进入递归,
singleChannelWithNewVids对象被videos数组转储 - 当没有更多
res.data.nextPageToken响应时,我将返回那个有点大的singleChannelWithNewVids对象。 - 我的
console.log(singleChannelWithNewVids, 'RETURING')行记录此对象存在 - 但是当
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