const tryToGetResult = async () => {
const networkResult = await someNetworkRequest();
console.log(networkResult); //Make sure networkResult is resolved to an object
return networkResult;
}
const result = tryToGetResult();
networkResult
已经是一个对象,并且流程已经通过使用async/await
. 但我不明白为什么const result = tryToGetResult()
,result
这里的变量变成了 a promise
?
我知道可以通过添加await
as来解决这种情况const result = await tryToGetResult();
,我只是不明白为什么需要在await
这里执行流程显然是正确的?
标记为 async 的函数总是返回一个 Promise
来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
异步函数是通过事件循环异步操作的函数,使用隐式 Promise 返回其结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句