我有一个获取方法:
number = 8, result = 0
这被称为:
async fetch {
return await axios.get('/api/users');
}
但是日志是 let data = this.fetch();
console.log(data);
- 如何在方法调用后获取日志以记录数据而不是承诺,我认为 async/await 会等待承诺解决?
答案 0 :(得分:1)
async fetch {
const res = await axios.get('/api/users');
return await res.data;
}
答案 1 :(得分:0)
要从 promise 中读取数据,您必须等待它完成执行。您可以使用 .then()
函数记录数据。
let data = this.fetch();
data.then(result => {
console.log(result);
});
您可以进一步了解 Promise here。