简体   繁体   English

为什么异步和等待未在函数外部返回已解决的承诺?

[英]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.

相关问题 为什么这个异步/等待 function 返回 promise? - Why does this async/await function return a promise? 为什么这在异步 function 之外等待工作? - Why does this await outside async function work? 为什么'await'触发'.then()'在'async'函数返回的代理上? - Why does 'await' trigger '.then()' on a Proxy returned by an 'async' function ? 使用 async/await 未解决 Promise - Promise is not resolved using async/await javascript“await”是否会立即暂停它正在执行的操作并在承诺解决后恢复异步功能? - Does javascript "await" pauses immediately whatever it is doing and resumes the async function once a promise is resolved? JavaScript异步函数,当没有返回值的情况下,返回时应答应解决 - JavaScript async function, when is returned promise resolved in the case of no return value 使用已解析的 promise 异步等待更新对象数组 - Updating an array of objects with a resolved promise async await Promise 嵌套在异步 function 中 - 为什么不只使用异步/等待? - Promise nested inside async function - why not working only with async/await? 为什么异步等待 function 返回一个值,而 promise function 返回一个未定义? - Why does async await function returns a value while promise function returns an undefined? 异步功能何时会解决? - When is async function promise resolved?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM