【发布时间】:2019-05-15 00:21:00
【问题描述】:
我从a couple 的similar questions 了解到,要在顶层使用async await,需要封装匿名函数。
我正在尝试初始化几个数据库连接,但语法不正确。
我尝试了以下方法:
let dbs = Promise.all(async () => {
await sqlite3.open("./db1.sqlite", { Promise }),
await sqlite3.open("./db2.sqlite", { Promise })
}
)
let [db_in, db_out] = dbs
失败:
evalmachine.<anonymous>:16
let [db_in, db_out] = dbs
^
TypeError: dbs is not iterable
和
async function init_dbs() {
const [db_in, db_out, abstract_queue] = await Promise.all([
sqlite3.open("./db1.sqlite", { Promise }),
sqlite3.open("./db2.sqlite", { Promise })
]);
let result = await [db_in,db_out]
}
const [db_in, db_out] = init_dbs().then(() => [db_in, db_out])
返回
evalmachine.<anonymous>:44
const [db_in, db_out] = init_dbs().then(() => [db_in, db_out])
^
TypeError: init_dbs(...).then is not a function or its return value is not iterable
什么是正确的语法?
【问题讨论】:
标签: javascript node.js sqlite async-await