【发布时间】:2022-02-04 02:43:53
【问题描述】:
我必须使用async/await 覆盖地图。
我想知道,如果我等待主要的Promise.all,它是否也会等待重叠的承诺?
const upsertPromises = body.schedules.map(async schedule => {
const createdSchedule = await this.addSchedule(briefId, schedule.week_day.id)
schedule.exclusions.map(async exclusion => {
await this.addScheduleExclusions(createdSchedule.id, exclusion.from.id, exclusion.to.id)
})
})
await Promise.all(upsertPromises)
在此代码上,this.addScheduleExclusions 是否在最后一行 Promise.all(upsertPromises) 之后完成?
【问题讨论】:
-
.map()仅在其函子返回某些内容时才有意义,因此在每个函子内添加一对或返回 ... 一个。upsertPromises将是一个或多个 promise 数组。如果您只对完成而不是值感兴趣,那么只需等待(或返回)Promise.all(upsertPromises.flat())(您可能需要 polyfill 用于 .flat())。
标签: node.js async-await promise