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併發