[英]Why does async and await not returned a resolved promise outside of the function?
I have an async function called getCat with await being used inside of it. 我有一个名为getCat的异步函数,其中正在使用await。 When the console log of the response gets executed inside of its function then it logs a resolved promise expected.
当响应的控制台日志在其功能内部执行后,它将记录一个预期的已解决承诺。 But if I call this from another function displayCat and log the result from the entire getCat function then it returns a promise.
但是,如果我从另一个函数displayCat调用此函数并记录整个getCat函数的结果,则它将返回一个Promise。 But the result should be the same at this point so why am I getting two different types of console log responses?
但是,此时的结果应该是相同的,那么为什么我会得到两种不同类型的控制台日志响应?
var getCat = async () => {
const result = await fetch('https://cataas.com/cat')
console.log('response: ', result);
return result;
}
var displayCat = () => {
console.log('getCat() : ', getCat())
}
异步函数隐式返回Promise,因此需要像其他任何一样等待它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.