异步编程与异步/等待的区别

huangapple go评论80阅读模式
英文:

Difference Between Asynchronous Programming and Async/Await

问题

异步编程和async/await之间有什么区别吗?

实际上,我了解async/await,我正在寻找异步编程和async/await之间的区别,但我找不到合适的答案。甚至我不知道它们是否相同,或者是否有任何区别。因此,出于这个原因,我在这里提出了这些问题。

英文:

Is there any difference between asynchronous programming and async/await?

Actually, I am aware with async/await and I search for find the difference between asynchronous programming and async/await, but I am not able to find proper answer. Even I don't know both are same or is there any difference. So, for that reason I raise this questions here.

答案1

得分: 2

Async/await 是 Node.js 中进行异步操作编程的一种方法。它是一种 JavaScript 语言特性,在某些情况下使得使用 promises 编程更加容易。

在 Node.js 中,还有其他异步编程的方法,包括使用普通回调、事件,或者只是使用 promises 的 .then().catch() 方法,这些方法都在 JavaScript 语言引入 async/await 之前被广泛使用。

英文:

Async/await is just one method of programming with asynchronous operations in nodejs. It's a Javascript language feature that makes programming with promises easier in some circumstances.

There are other methods of asynchronous programming in nodejs using plain callbacks or events or just using .then() and .catch() with promises, all of which were used before we even had async/await in the Javascript language.

答案2

得分: 0

异步编程是一种技术,它使您的程序能够启动一个可能运行时间较长的任务,同时仍然能够响应其他事件。

如果有人认为,除非您的代码设计允许同时运行多个任务,否则您实际上并没有编写异步代码。

如果您有一个任务是使用fetch()来获取URL内容的,那就是一个可以在执行其他JavaScript代码时同时运行的示例。

然而,可以使用Promise(直接或通过使用async/await [语法糖] 1)来确保永远不会同时运行两个任务,因为不会触发在后台运行任务的浏览器调用。一个示例是编写一个阻塞队列,其中使用async/await更清晰地组织代码。代码仍然是单线程的,永远不会同时执行两个任务。

最重要的是,您可以使用Promise编写异步代码,但除非要求浏览器在后台执行某些操作(或使用Web Workers),否则实际上不会有多个任务同时执行。

英文:

> Asynchronous programming is a technique that enables your program to
> start a potentially long-running task and still be able to be
> responsive to other events while that task runs

Source: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing

One could argue that you're not really writing asynchronous code unless your code is designed to allow more than one task to run simultaneously.

If you have a task that is retrieving the contents of a URL using fetch(), that would be an example of a task that can run while you're also executing other javascript.

However, it's possible to use promises (either directly or through the use of async/await syntactic sugar) such that there are never two tasks that run simultaneously, because no browser calls that cause tasks to run in the background are ever made. An example would be to write a blocking queue, where async/await is used to more clearly structure the code. The code would still be single threaded, and two tasks would never execute simultaneously.

The bottom line is that you can write asynchronous code using promises, but you won't actually ever have multiple tasks executing simultaneously unless you ask the browser to do something in the background (or use web workers).

huangapple
  • 本文由 发表于 2023年1月6日 14:42:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定