简体   繁体   English

"为什么 Async 总是返回一个 Promise?"

[英]Why Does Async Always Return a Promise?

This question is theoretical - I have no concrete problem to solve.这个问题是理论上的——我没有具体的问题要解决。

With that said, why does the async<\/code> keyword wrap the return value of an async function in a promise?话虽如此,为什么async<\/code>关键字将异步函数的返回值包装在 Promise 中? What's the point?重点是什么? Is it ONLY because the await<\/code> expression expects a promise?仅仅是因为await<\/code>表达式需要一个承诺吗? Or is there some meaning \/ use behind this decision?或者这个决定背后有什么意义\/用途?

"

I thought i'd answer this primarily because async in Javascript used to confuse the hell out of me, and all of a sudden it snapped, so i hope this analogy may help this happen for you.我想我会回答这个问题,主要是因为 Javascript 中的 async 曾经让我感到很困惑,突然间它就崩溃了,所以我希望这个类比可以帮助你实现这一点。

You have an async event.你有一个异步事件。 This could be anything, getting something from a server, doing something in the browser that takes time, training a machine learning model (!), executing a function or method that uses a setTimeout etc.这可能是任何事情,从服务器获取某些东西,在浏览器中做一些需要时间的事情,训练机器学习模型(!),执行使用 setTimeout 的函数或方法等。

The beauty of Javascript and a key reason it works so well for the browser is that it uses the processor thread it runs on in a very clever way that stops the thread from getting blocked by processes that take time (like the ones mentioned above) Javascript 的美妙之处以及它在浏览器中运行良好的一个关键原因是它以一种非常聪明的方式使用它运行的处理器线程,从而阻止线程被需要时间的进程阻塞(如上面提到的那些)

Many other languages, for example Ruby run on more than one thread.许多其他语言,例如 Ruby,在多个线程上运行。 It is possible to use service workers to run processes on multiple threads in javascript but that is outside the scope of this answer!可以使用服务工作者在 javascript 中的多个线程上运行进程,但这超出了此答案的范围!

The async nature of the JS event loop allows the thread to 'go off' and do something else while it is waiting for a process to finish. JS 事件循环的异步特性允许线程在等待进程完成时“关闭”并执行其他操作。

The problem with this from a programming point of view is that it is possible for something in the code that relies on the result of a blocking event to get 'undefined' as a result of the event if it doesn't wait for the event to finish before it tries to use the result of it.从编程的角度来看,这样做的问题是,如果代码中依赖于阻塞事件结果的某些东西不等待事件发生,则它可能会因为事件而得到“未定义”。在它尝试使用它的结果之前完成。 Take this piece of code below拿下面这段代码

let scopedVariable
console.log('the code has started')
setTimeout(() => {
  scopedVariable="I am the result of some async process"
}, 5000);
console.log(scopedVariable)

The async<\/code> and await<\/code> operators are just syntactic sugar that hide the underlying use of promises to implement asynchronous code. async<\/code>和await<\/code>运算符只是语法糖,它们隐藏了 promise 的底层使用来实现异步代码。

Using async<\/code> before a function definition makes the function return a promise that resolves to the function's return value, rather than returning normally.在函数定义之前使用async<\/code>会使函数返回一个解析为函数返回值的承诺,而不是正常返回。

Using await<\/code> before an asynchronous function call suspends the current function until the promise that it returns is resolved.在异步函数调用之前使用await<\/code>会挂起当前函数,直到它返回的 promise 被解决。 It's basically equivalent to wrapping the remainder of the function in an anonymous function, and using that as the .then()<\/code> callback of the promise.它基本上等同于将函数的其余部分包装在一个匿名函数中,并将其用作 promise 的.then()<\/code>回调。

For more information between the relationship, see How to translate Promise code to async await<\/a>有关关系的更多信息,请参阅如何将 Promise 代码转换为异步等待<\/a>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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