简体   繁体   中英

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. 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. 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,因此需要像其他任何一样等待它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM