Javascript async/await 总是返回承诺
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43422932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
async/await always returns promise
提问by user3309314
I'm trying async/await functionality. I have such code imitating a request:
我正在尝试异步/等待功能。我有这样的代码模仿请求:
const getJSON = async () => {
const request = () => new Promise((resolve, reject) => (
setTimeout(() => resolve({ foo: 'bar'}), 2000)
));
const json = await request();
return json;
}
When I use the code in this way
当我以这种方式使用代码时
console.log(getJSON()); // returns Promise
it returns a Promise
它返回一个 Promise
but when I call this line of code
但是当我调用这行代码时
getJSON().then(json => console.log(json)); // prints { foo: 'bar' }
it prints json as expected
它按预期打印 json
Is it possible to use just code like console.log(getJSON())? What don't I understand?
是否可以只使用像这样的代码console.log(getJSON())?我不明白什么?
回答by Pedro Castilho
Every asyncfunction returns a Promiseobject. The awaitstatement operates on a Promise, waiting until the Promiseresolves or rejects.
每个async函数都返回一个Promise对象。该await语句对 a 进行操作Promise,直到Promiseresolves 或rejects。
So no, you can't do console.logon the result of an async function directly, even if you use await. Using awaitwill make your function wait and then return a Promisewhich resolves immediately, but it won't unwrap the Promisefor you. You still need to unwrap the Promisereturned by the asyncfunction, either using awaitor using .then().
所以不,你不能console.log直接处理异步函数的结果,即使你使用await. 使用await将使您的函数等待,然后返回一个Promise立即解析的,但它不会Promise为您解包。您仍然需要使用 using或 using解开函数Promise返回的结果。asyncawait.then()
When you use .then()instead of console.logging directly, the .then()method makes the result of the Promise available to you. But you can't get the result of the Promisefrom outsidethe Promise. That's part of the model of working with Promises.
当您直接使用.then()而不是console.logging 时,该.then()方法使 Promise 的结果对您可用。但是你不能Promise从Promise之外得到结果。这是使用 Promise 的模型的一部分。
回答by Ozan
Return value of an async functionwill always be an AsyncFunction Object, which will return a Promisewhen called. You can not change that return type. The point of async/awaitis to easily wait for other async process to complete inside an async function.
异步函数的返回值始终是AsyncFunction Object,Promise调用时将返回 a 。您无法更改该返回类型。重点async/await是在异步函数内轻松等待其他异步进程完成。
回答by TheMonarch
A function defined with asyncalways returns a Promise. If you return any other value that is not a Promise, it will be implicitly wrapped in a Promise.The statement const json = await request();unwraps the Promisereturned from requestto a plain object { foo: 'bar' }. This is then wrapped in a Promisebefore being returned from getJSONso a Promiseis what you ultimately get when you call getJSON(). So to unwrap it, you can either call getJSON().then()like you've done or do await getJSON()to get the resolved value.
用 定义的函数async总是返回一个Promise。如果您返回任何其他不是 a 的值,Promise它将被隐式包装在 a 中Promise。该语句const json = await request();将Promise返回的 from 解包request为一个普通对象{ foo: 'bar' }。然后在Promise返回之前将其包装在 a中,getJSON因此 aPromise是您调用getJSON(). 所以要解开它,你可以getJSON().then()像你所做的那样调用或await getJSON()获取解析值。

