rua.kr
rua.kr

Reputation: 86

Waiting for a promise natively

What I want:

// db.js
const mariadb = require('mariadb');
module.exports = {
 conn: await mariadb.createConnection({ ... }) // mariadb returns a connection with a promise
};
// foo.js
const conn = require('./db').conn;

What am I doing:

// db.js
const mariadb = require('mariadb');
module.exports = {
 getConn: mariadb.createConnection({ ... }) // mariadb returns a connection with a promise
};
// foo.js
const getConn = require('./db').getConn;
(async()=>{
    const conn = await getConn;
    // ...
})();

I feel it's waste.
Is there any way to wait for a promise until export?

Upvotes: 1

Views: 56

Answers (1)

Josh Wulf
Josh Wulf

Reputation: 4877

Only with top-level await, which is not yet supported in Node.

See also why you may not want to do this.

Upvotes: 1

Related Questions