gpt4 book ai didi

node.js - 本地等待 promise

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:43 26 4
gpt4 key购买 nike

我想要什么:

// 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;

我在做什么:

// 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;
// ...
})();

我觉得这是浪费。
有没有什么办法可以等待 promise 直到导出?

最佳答案

仅限top-level await ,Node 尚不支持。

另请参阅why you may not want to do this .

关于node.js - 本地等待 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60464462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com