异步异步/等待

时间:2018-10-28 19:23:59

标签: javascript node.js asynchronous promise async-await

我有以下代码行:

MongoClient.connect(customer_db_url)
    .then(async (customerDB) => {
        const callDB = await MongoClient.connect(call_db_url);
        return await customerDB.listCollections({ name: { $regex: /contact_/ } })
            .forEach(async (collection) => {
                await console.log(collection.name);
            });
    })
    .then(() => console.log('Done!'))
    .catch((error) => console.log(error.message));

控制台在内部Done!之前先记录collection.name。我还没有我认为async await关键字将确保异步函数在执行下一个函数之前得到满足。即使我没有回到那里,但毕竟forEach,结果还是一样。

0 个答案:

没有答案