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

相關文章
相關標籤/搜索