3

I have following code which doesn't work due to syntax error (await outside an async function)

  1. how do I define a variable with await and export it?

  2. When I define a variable like this and import it from other files, does the variable gets created only once (when file is first read?) or gets created everytime when it's imported?

Code..

import _ from 'lodash'
import remoteConfig from '@react-native-firebase/remote-config'

class RemoteConfig {
  constructor() {
    if (__DEV__) {
      //Fetch, no cache. activate
      remoteConfig().fetchAndActivate(0)
    } else {
      //Fetch, cache for 5 minutes and activate
      remoteConfig().fetchAndActivate()
    }
  }

  static async build() {
    await remoteConfig().setConfigSettings({
      minimumFetchIntervalMillis: 300000,
    })

    return new RemoteConfig()
  }

  setDefaults(defaults) {
    remoteConfig().setDefaults(defaults)
  }

  getValues(keys) {
    return _.pick(remoteConfig().getAll(), keys)
  }

  getValue(key) {
    return remoteConfig().getValue(key)
  }
}

export let firebaseConfig = await RemoteConfig.build()

I'm using it with import {firebaseConfig} from path/to/thefile

1
  • @yash I think you're looking to use a singleton pattern. since you're already using async functions inside your constructor, why not just call your build() method from there and just export a single instance of your class? Commented Jul 27, 2020 at 3:46

2 Answers 2

1
  1. await can be only used in async function. It's not possible to export a variable with await syntax.

  2. Since the export with await is not possible, it's difficult to say if the RemoteConfig.build() will be called each time. If we assume, you wrote firebaseConfig = RemoteConfig.build();. The function will be called one time when the module is evaluated.

Here is a workaround:

You can define a function to set the firebaseConfig and call it at the time of app start.

So:

export let firebaseConfig;

export async function setFirebaseConfig() {
    firebaseConfig = await RemoteConfig.build();
}

This will allow you to not call the RemoteConfig.build() more than once. Also the firebaesConfig can be exported without await syntax.

Sign up to request clarification or add additional context in comments.

7 Comments

if OP was not looking to execute a function in the import, could an async self invoking function be used in its stead?
RemoteConfig.build() won't be evaluated multiple times. Module code is evaluated first time when it is imported, then the same exports returned to all importers.
I am not sure what you mean by OP was not looking to execute a function in the import
I dont think so. Because if he imports the config from a child component, the function will be called each time the component appears. Just calling it at app bootstrap will initialize the firebaseConfig and it can be used across the app.
@eugene, it's only once.
|
0

To make this work, you have to wait for the "Top-level await" proposal (currently at Stage 3), that allows doing so (with a transpiler like Babel, you can use it now). It would allow exactly the same code that you have.

Until then, the only way to achieve a variable to exported like this to make the variable's value a promise, that all importers have to await on its own.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.