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

前端 未结 2 1241
陌清茗
陌清茗 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; });
    }
    

提交回复
热议问题