Asynchronous map function that await's returns Promise instead of value

前端 未结 2 1240
陌清茗
陌清茗 2021-02-05 17:01

I have this code

async function addFiles(dir,tree) {
  return (await readDir(dir))
    .map(async (name) => {await readDir(dir); return name;})
}
相关标签:
2条回答
  • 2021-02-05 17:58

    If you're using bluebird you can use this cleaner, shorter syntax with Promise.map

    async function addFiles(dir, tree) {
      const files = await readDir(dir);
      return Promise.map(files, async (name) => { await readDir(dir); return name; });
    }
    
    0 讨论(0)
  • 2021-02-05 18:04

    try

    async function addFiles(dir,tree) {
      const files = await readDir(dir)
      await Promise.all(files.map(async (name) => {await readDir(dir); return name;})
    }
    
    0 讨论(0)
提交回复
热议问题