Asynchronous JavaScript
Try code in the playgroundUnderstand the concept
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.
Important ideas
-
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 |
Work through the example
async function getTotal() {
const price = await Promise.resolve(100);
return price + 20;
}
getTotal().then(console.log); // 120Can you explain it without the notes?
How does async JavaScript run?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
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.
Turn this concept into a clear answer.
- Promises coordinate results; they do not create a new JavaScript thread or automatically make heavy work non-blocking.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “How does async JavaScript run?”. Examples are study fragments; framework behavior can depend on your version.