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循环作:html

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

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

 

详见:https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop并发

   http://www.ruanyifeng.com/blog/2015/05/async.htmlasync

相关文章
相关标签/搜索