【发布时间】:2020-07-27 03:27:01
【问题描述】:
我有以下代码由于语法错误而无法工作(在异步函数之外等待)
-
如何用 await 定义一个变量并导出它?
-
当我定义一个这样的变量并从其他文件中导入它时,该变量是只创建一次(第一次读取文件时?)还是每次导入时都创建?
代码..
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()
我正在与import {firebaseConfig} from path/to/thefile一起使用它
【问题讨论】:
-
@yash 我认为您正在寻找使用单例模式。既然你已经在构造函数中使用了异步函数,为什么不从那里调用你的 build() 方法并导出你的类的单个实例?
标签: javascript