【发布时间】:2021-11-04 08:34:31
【问题描述】:
由于 Async 总是返回 Promise,我们必须解决它来获取值。我需要导出它的值(返回的对象),以便我们可以在另一个模块中使用它。
export const getClient = async () => {
return await HttpService.getValueFromSettings('durl').then((response) => {
if(isValidResponse(response)) {
endpoint = response.data;
export const client = createClient(response.data);
//This is where getting error that we can not export in side the function
}
}
})
}
我需要在另一个模块中使用客户端。
我尝试在调用之前声明并初始化客户端对象,但没有成功:
export let client = null;
export const getClient = async () => {
return await HttpService.getValueFromSettings('durl').then((response) => {
if(isValidResponse(response)) {
endpoint = response.data;
client = createClient(response.data);
return client;
}
}
})
}
【问题讨论】:
-
为什么不创建一个方法并在每个需要的地方调用它?
-
因为我必须通过 API 获取数据,而我需要使用的地方显示错误,因为他们不能等到方法返回结果。
标签: javascript asynchronous async-await