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);
  }
}

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM