[英]Why we have to use keyword async and await to making it synchronus in node, when the keyword is literally async?
So I was thinking that ok node is asynchronous (meaning multiple lines of code execute at the same time), then why we use the keyword async( which literally means asynchronous) to wait for a task to be done by keyword await (that is making it synchronous).所以我在想ok节点是异步的(意味着多行代码同时执行),那么为什么我们使用关键字async(字面意思是异步)来等待关键字await完成的任务(即它是同步的)。 Why there is not a keyword sync , because that's what we are doing?为什么没有关键字sync ,因为这就是我们正在做的? Can someone please explain the logic of keyword async?有人可以解释关键字异步的逻辑吗? I am so confused.我感到很困惑。
The async
keyword enables the use of await
which is just a more streamlined syntax for writing code with promises and asynchronous operations. async
关键字允许使用await
,这只是一种更简化的语法,用于编写带有 Promise 和异步操作的代码。 It does not make anything synchronous.它不会使任何东西同步。 It just allows you to use similar programming techniques as synchronous code, but it is all still asynchronous.它只是允许您使用与同步代码类似的编程技术,但它仍然是异步的。
Can someone please explain the logic of keyword async?有人可以解释关键字异步的逻辑吗? I am so confused.我感到很困惑。
When you use the async
keyword to declare a function, you are creating a different type of function that ALWAYS returns a promise.当您使用async
关键字声明 function 时,您正在创建一种不同类型的 function,它总是返回 promise。 This is useful only for asynchronous programming - thus the async
keywords's affiliation with the term "asynchronous".这仅对异步编程有用——因此async
关键字与术语“异步”的关联。
Further, the async
keyword does not make anything synchronous.此外, async
关键字不会使任何东西同步。 It does allow you to use a more synchronous-like programming model which is a lot simpler to use, particular for things like conditional branches in asynchronous code, but nothing that was asynchronous actually becomes synchronous.它确实允许您使用更类似于同步的编程 model,它使用起来要简单得多,特别是对于异步代码中的条件分支之类的东西,但实际上没有什么异步变成同步的。
Node.js is not "asynchronous," but instead, Node.js has a set of asynchronous APIs. Node.js 不是“异步的”,而是 Node.js 具有一组异步 API。
To answer your question about async/await -
async/await is just syntactic sugar as it uses promises under the hood.回答您关于async/await -
async/await 只是语法糖,因为它在底层使用了 Promise。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.