是否可以从 ES6 类中导出 await 函数的值?例如,我有一个类,其函数名为 getDetails
看起来像这样
class Config {
static async getDetails() {
let response = await fetch('url/where/details/are')
let data = await response.json()
return data;
}
}
export { Config }
当我将此类导入另一个文件并调用 User.getDetails()
时,返回的是Promise {<pending>}
.有没有办法让堆栈先等待 API 的响应?
最佳答案
为什么不在父文件中也使用 await
?
// your async function
const res = await User.getDetails()
// here you have res
或者,您可以使用 .then
User.getDetails()
.then(res => {
// use res here
})
关于javascript - 导出从 ES6 中的异步/等待函数返回的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48348353/