04月24, 2020

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)
  }));
}
async function dbFuc(db) {
  let docs = [{}, {}, {}];
  for (let doc of docs) {
    await db.post(doc);
  }
}
let a = [1, 2, 34, 56]

// a.forEach(async function (v) {
//     await aa(v).then(console.log(v,'====>'))
// })

function aa(val) {
    return new Promise((resolve) => {
        setTimeout(function () {
            console.log(val)
            resolve('done')
        }, 2000)
    })
}

(async function () {
    for (value of a) {
        await aa(value)
    }
    /*for (let i = 0; i < a.length; i++) {
        await aa(a[i])
    }*/
})()

本文链接:https://blog.hijs.cc/post/await.html

-- EOF --

Comments