在 JavaScript 中导出异步值的最佳做法是什么?
What's the best practice around exporting an async value in JavaScript?
在下面的代码片段中,我试图导出一个依赖于异步操作的值。这是最好的方法吗?就是感觉有点奇怪。
我会喜欢任何建议!谢谢!
import ohm from "ohm-js";
export default await (async () => {
let response = await fetch("./grammar.ohm");
let source = await response.text();
return ohm.grammar(source);
})();
您不需要 async IIFE:
import ohm from "ohm-js";
let response = await fetch("./grammar.ohm");
let source = await response.text();
export default await ohm.grammar(source);
在下面的代码片段中,我试图导出一个依赖于异步操作的值。这是最好的方法吗?就是感觉有点奇怪。
我会喜欢任何建议!谢谢!
import ohm from "ohm-js";
export default await (async () => {
let response = await fetch("./grammar.ohm");
let source = await response.text();
return ohm.grammar(source);
})();
您不需要 async IIFE:
import ohm from "ohm-js";
let response = await fetch("./grammar.ohm");
let source = await response.text();
export default await ohm.grammar(source);