简体   繁体   English

当关键字字面上是异步时,为什么我们必须使用关键字 async 和 await 使其在节点中同步?

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

相关问题 异步 function 和 await 关键字 - Async function and await keyword async / await链中的所有功能都必须使用async / await关键字吗? - Do all functions in an `async/await` chain have to use the `async/await` keyword? 为什么我必须将 async 关键字放在具有 await 关键字的函数中? - Why do I have to put async keyword to functions which have await keywords? 为什么需要使用await关键字来调用异步方法 - Why the need of using await keyword for calling async method 不能在 VUEJS 中的异步 function 之外使用关键字“等待” - Can not use keyword 'await' outside an async function in VUEJS VueJS - 不能在异步 function 之外使用关键字“等待” - VueJS - Can not use keyword 'await' outside an async function 内部没有await关键字的异步函数 - async function without await keyword inside 在没有 async 关键字的全局范围内使用 await - using await on global scope without async keyword 可以在没有 await 关键字的情况下调用异步函数吗? 如果我们在没有等待的情况下调用会发生什么? - Is possible to call async function without await keyword? and what happens if we call without await? js async / await在使用此关键字时引发错误 - js async/await throws error on using this keyword
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM