我有两个这样的函数:
async getMatches() {
try {
const result = await readRowsFromDb('SELECT * FROM matches');
console.log(result);
} catch (error) {
console.error(error);
}
}
async readRowsFromDb(query) {
const db = await sqlite.open(this.DATABASE_PATH);
const promise = db.all(query);
return promise;
}
getMatches();
Run Code Online (Sandbox Code Playgroud)
问题是我找不到合适的地方放线
db.close()
Run Code Online (Sandbox Code Playgroud)
AFAIK,我应该在得到结果后关闭数据库,但结果在不同的函数中,关闭 getMatches 内的数据库似乎不正确。
我尝试在像这样返回 readRowsFromDb 中的承诺之前关闭数据库
const promise = db.all(query);
db.close();
return promise;
Run Code Online (Sandbox Code Playgroud)
有效。但我仍然觉得有些不对劲,因为我在调用查询后立即关闭数据库连接而不是等待它完成,它不应该起作用。
请指教。
我有两个钩子。一个叫useLocalStorage,另一个叫useQuery。基于输入道具,我想有条件地使用其中之一。
我简单写一下可以吗const [value, setValue] = localStorage ? useLocalStorage() : useQuery()?