admin管理员组文章数量:1296290
What I want to implement is that get a data from the database, which the getter method is async, then export a const variable based on the promise returned value.
The code is like this:
import {Storage} from "@ionic/storage";
//...
storage.get("setup_done").then((val)=>{
export const FirstRunPage = val?'valueA':'valueB';
})
However, I get an error message that:
Modifiers cannot appear here
why could that happen?
What I want to implement is that get a data from the database, which the getter method is async, then export a const variable based on the promise returned value.
The code is like this:
import {Storage} from "@ionic/storage";
//...
storage.get("setup_done").then((val)=>{
export const FirstRunPage = val?'valueA':'valueB';
})
However, I get an error message that:
Modifiers cannot appear here
why could that happen?
Share Improve this question asked Dec 22, 2017 at 19:19 AlexLuoAlexLuo 4871 gold badge4 silver badges13 bronze badges 2- 2 But why?! Why not do the request in a different module and use it there? It's impossible to 'export at a later time'. A module is evaluated and its exports are loaded into another. There's now waiting. – Andrew Li Commented Dec 22, 2017 at 19:22
- 1 This, it wouldn't make sense to dynamically export something there. You might instead want to export an object that will change one of its internal properties once things are available, or better yet, export a Promise that returns the value. – zeh Commented Dec 22, 2017 at 19:25
1 Answer
Reset to default 6All exports have to appear at the top level, and there's not really a way to do some kind of asynchronous export like you want.
The way I see it you have two options. The first, and probably the simplest, is to just export the promise itself:
import {Storage} from "@ionic/storage";
//...
export const FirstRunPagePromise = storage.get("setup_done").then((val)=>{
return val ? 'valueA' : 'valueB';
})
That means that consumers of the module will have to access the value using .then
, just like with any other promise.
Your second option is to assign the value to a variable when it resolves, and export a getter function for that variable:
import {Storage} from "@ionic/storage";
//...
let FirstRunPage: string;
storage.get("setup_done").then((val)=>{
FirstRunPage = val ? 'valueA' : 'valueB';
});
export function getFirstRunPage() {
return FirstRunPage;
}
You have to use the getter because importing the variable itself will give you a copy, which won't update when the promise resolves.
This approach means you can access the value synchronously, but if you access it too early, it will be undefined
. So all your code would have to check to see if the value exists first, then use it. That or you have to know as the developer that whatever code accesses the value will only be running after the promise has been resolved.
Personally I remend option 1, but I've done both before and ultimately it depends on your use case. Do what makes sense for your situation.
本文标签: javascriptTypeScript how to export const in a promiseStack Overflow
版权声明:本文标题:javascript - TypeScript how to export const in a promise - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741635043a2389592.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论