英文:
How to make one async db call and save output for subsequent functions?
问题
我试图尽量减少数据库查询。目标是进行一次查询,然后在其他函数中使用数据。到目前为止,我有以下代码:
async function GetCoverage(scroll_path) {
const apiName = "xxxx";
const path = "/scrolls/" + scroll_path;
const myInit = {
headers: {},
response: false,
};
const response = await API.get(apiName, path, myInit);
console.log("response:", response);
return response.Items;
}
let dataGlobal;
const getData = async () => {
const response = await GetCoverage("all");
dataGlobal = response;
return response;
};
(async () => {
await getData();
console.log("dataGlobal:", dataGlobal);
})();
问题是每次调用await getData()
都会触发另一个数据库查询。我该如何避免这种情况?
英文:
I am trying to keep db queries to a minimum. The goal is to make one query and use data in other functions. So far, I have this code:
async function GetCoverage(scroll_path) {
const apiName = "xxxx";
const path = "/scrolls/" + scroll_path;
const myInit = {
headers: {},
response: false,
};
const response = await API.get(apiName, path, myInit);
console.log("response:", response);
return response.Items;
}
let dataGlobal;
const getData = async () => {
const response = await GetCoverage("all");
dataGlobal = response;
return response;
};
(async () => {
await getData();
console.log("dataGlobal:", dataGlobal);
})();
The problem is every await getData()
invocation drives another db query. How can I avoid that?
答案1
得分: 2
你基本上想要一个惰性初始化的持久化 Promise 结果。
类似以下代码应该足够了...
let dataGlobal;
const getData = () => (dataGlobal ??= GetCoverage("all"));
第一次调用 getData
时,它将把由 GetCoverage()
返回的 Promise 赋给 dataGlobal
并返回它。
随后的调用将返回已分配的 Promise。
这是一个使用虚拟的 GetCoverage
的快速演示
// 模拟
const GetCoverage = () => {
console.log("GetCoverage called");
return new Promise((r) => {
setTimeout(r, 1000, Math.random());
});
}
let dataGlobal;
const getData = () => (dataGlobal ??= GetCoverage("all"));
// 进行并行调用
getData().then(console.log.bind(null, "Result #1:"));
getData().then(console.log.bind(null, "Result #2:"));
getData().then(console.log.bind(null, "Result #3:"));
另请参阅Nullish 合并赋值 (??=)。
英文:
You basically want a lazily initiated, persistent promise result.
Something like this should suffice...
let dataGlobal;
const getData = () => (dataGlobal ??= GetCoverage("all"));
The first time getData
is called, it will assign the promise returned by GetCoverage()
to dataGlobal
and return it.
Any subsequent calls will return the already assigned promise.
Here's a quick demo using a fake GetCoverage
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// mock
const GetCoverage = () => {
console.log("GetCoverage called");
return new Promise((r) => {
setTimeout(r, 1000, Math.random());
});
}
let dataGlobal;
const getData = () => (dataGlobal ??= GetCoverage("all"));
// Make parallel calls
getData().then(console.log.bind(null, "Result #1:"));
getData().then(console.log.bind(null, "Result #2:"));
getData().then(console.log.bind(null, "Result #3:"));
<!-- end snippet -->
See also Nullish coalescing assignment (??=)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论