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
Add a ment  | 

1 Answer 1

Reset to default 6

All 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