Home > Software design >  Difference Between Asynchronous Programming and Async/Await
Difference Between Asynchronous Programming and Async/Await

Time:01-06

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.

CodePudding user response:

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.

CodePudding user response:

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).

  • Related