How does async JavaScript run?
Practice this questionDefinition:
Asynchronous JavaScript lets a program start work that may finish later without waiting synchronously for the result. A promise represents eventual completion, and async/await provides a readable way to work with promises. The runtime queues continuations and runs them when the current synchronous work has finished and the relevant checkpoints are reached.
Key points:
-
Synchronous code runs first; queued promise callbacks run after the current code finishes.
-
awaitpauses that async function, not the whole JavaScript thread. -
Making a CPU-heavy function
asyncdoes not stop it from blocking. -
An async function always returns a promise, including when it returns a plain value.
-
Use
try/catcharound an awaited operation to handle a rejection; synchronoustry/catchcannot catch an unrelated later callback error. -
The executor passed to
new Promise(...)runs immediately;.then(...)callbacks run asynchronously.
Comparison:
| Point | Promise with .then | async / await |
|---|---|---|
| What it provides | A future result and callbacks | Syntax for consuming promises |
| Error handling | .catch(...) | try/catch around await |
| Creates a new thread | No | No |
| Best readability | Short chains | Multi-step flows |
Example:
async function getTotal() {
const price = await Promise.resolve(100);
return price + 20;
}
getTotal().then(console.log); // 120Interview point:
- Promises coordinate results; they do not create a new JavaScript thread or automatically make heavy work non-blocking.