【发布时间】:2019-10-01 00:45:49
【问题描述】:
我正在查看this tutorial,它有一个名为 aa-sqlite 的库,以便用 async-await 替换 Promises() 语法。
我在 npm 上是 not seeing aa-sqlite。是否有另一种更新的异步等待 sqlite 语法?
这是我正在尝试使用标准 sqlite 库:
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database("tmp.db")
async function myfunc(db) {
let sql = "SELECT id id FROM TABLE LIMIT 2"
let res1 = await db.run(sql)
console.log(res1)
for (row of res1) {
console.log(row);
}
但这会产生
TypeError: res1 is not iterable
我不希望 res1 成为对象,而是结果的迭代器。如何在 ES7/ES8 中异步/等待 db.run 查询的结果?
【问题讨论】:
-
console.log(res1)记录了什么? -
数据库 { 打开:true,文件名:'tmp.db',模式:65542 }
-
@Mittenchops 您不能将
for ..of用于对象。见developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
我认为你的包 -
sqlite3不支持promise或async/await语法。在 ES6 中,您可以使用 Promise 包装基于回调的函数,它本质上是带有状态(待处理、已解决、已拒绝)的返回值。
标签: javascript node.js sqlite asynchronous async-await