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). Why there is not a keyword sync, because that's what we are doing? Can someone please explain the logic of keyword async? I am so confused.
-
1The `async` keyword does *not* make code synchronous. `async/await` is just a tool to make the syntax for interacting with promises nicer. It's still using promises. – Nicholas Tower Nov 20 '21 at 18:02
-
"*node is asynchronous (meaning multiple lines of code execute at the same time)*" that is *not* what asynchronous means. The word literally means "not synchronous" or roughly "not sequential", if you will (but that can also give a false impression). Synchronous code resolves each line *now* and then moves to the next one. Async code can resolve some lines *later*. Not "all at the same time" - that is multithreading/concurrency which is a *separate* topic. – VLAZ Nov 20 '21 at 18:10
-
Does this answer your question? [What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?](https://stackoverflow.com/questions/62196932/what-are-asynchronous-functions-in-javascript-what-is-async-and-await-in-ja) – TylerH Jan 25 '23 at 17:25
2 Answers
The async
keyword enables the use of await
which is just a more streamlined syntax for writing code with promises and asynchronous operations. 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. This is useful only for asynchronous programming - thus the async
keywords's affiliation with the term "asynchronous".
Further, the async
keyword does not make anything synchronous. It does allow you to use a more synchronous-like programming model which is a lot simpler to use, particularly for things like conditional branches in asynchronous control flow, but nothing that was asynchronous actually becomes synchronous.
As an example of how nothing becomes synchronous, run this example in the snippet and note the order of console.log()
statements. The async
function returns when it hits the first await
and the rest of the execution continues which the asynchronous operation is still going. This is a simulation with a timer, but that timer could be any other asynchronous operation such as an http request, a database operation, etc...
function delay(t) {
return new Promise(resolve => {
setTimeout(resolve, t);
});
}
async function run() {
console.log("in run() 1");
await delay(100);
console.log("in run() 2");
}
console.log("before run()");
run();
console.log("after run()");
You can see from running this little example, that the run()
function has its execution suspends as soon as you hit the await
and it returns allowing the other code after the function call to run. Then, sometime later when the timer fires and the delay()
promise is resolved, the run()
function picks up where it was suspended and executes the rest of the function body.
This is operationally the same as this code using .then()
instead of async
and await
:
function delay(t) {
return new Promise(resolve => {
setTimeout(resolve, t);
});
}
function run() {
console.log("in run() 1");
delay(100).then(() => {
console.log("in run() 2");
});
}
console.log("before run()");
run();
console.log("after run()");

- 683,504
- 96
- 985
- 979
Node.js is not "asynchronous," but instead, Node.js has a set of asynchronous APIs.
To answer your question about async/await -
async/await is just syntactic sugar as it uses promises under the hood.

- 125
- 10