其他分享
首页 > 其他分享> > for循环中使用async/await

for循环中使用async/await

作者:互联网

async function printFiles () {
  const files = await getFilePaths();

  await Promise.all(files.map(async (file) => {
    //耗时操作
    const contents = await fs.readFile(file, 'utf8')
    console.log(contents)
  }));
}

以上是并发操作,如果不想并发,使用for循环做:

async function dbFuc(db) {
  let docs = [{}, {}, {}];

  for (let doc of docs) {
    await db.post(doc);
  }
}

  

标签:files,const,await,循环,file,async,contents
来源: https://www.cnblogs.com/Ming2021/p/15727402.html