Nodejs implements async Synchronize blocking

console.log("a");

(async () => {

    await initDB();

})();


console.log("b");

such as the code above. The order of execution should be
a
b
initDB

the order I expect is

a
initDB
b

because initDB is the connection and initialization of the database, I hope all other operations will be done after this step is completed.

how is it implemented?

Mar.01,2021

drop all the following steps behind await initDB () , otherwise it will be useless for you to use await .

console.log('a');

(async () => {
    await initDB();
    console.log('b');
})();

you can also try

console.log('a');

await (async () => {

    await initDB();

})();

console.log('b');

this should not be necessary to write the latter into the function.

Menu