Asynchronous JavaScript, promises, and the event loop
ECMAScript 2026
Some browser operations take time. Synchronous code runs its current instructions before moving on. Asynchronous operations let other work continue while a timer, network response, or another result is pending.
On this page
What can run while a timer waits?
setTimeout is a browser API. It registers a function to run after at least the delay you give it, in milliseconds. The function is a callback, which means you pass it to be called later. The timer does not pause the remaining synchronous instructions.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
console.log("Start");
setTimeout(() => console.log("Timer"), 100);
console.log("End");
What to expect Start and End appear first. Timer appears later.
What does a Promise represent?
A Promise represents the eventual outcome of an operation. Pending means the result is still unknown. Fulfilled means the operation supplied a value. Rejected means it ended with a reason for failure. Use then() to register a handler for success and catch() to handle rejection.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const result = new Promise(resolve => {
setTimeout(() => resolve("Your order is ready."), 100);
});
result.then(message => console.log(message)).catch(error => console.error(error));
console.log("Waiting");
What to expect Waiting appears first. When the promise fulfills, its handler prints “Your order is ready.”.
Return a transformed result from then()
then() returns a new promise. If its handler returns a value, that value becomes the fulfillment value of the new promise. A second handler can read the transformed result.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
const product = Promise.resolve("Pen");
const message = product.then(value => "Ready: " + value);
message.then(value => console.log(value));
What to expect The second handler prints “Ready: Pen”.
Read a promise result with async and await
An async function always returns a promise. await waits for its input within that function. Other code outside the function can continue running. If the awaited promise rejects, try/catch can handle the failure.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
async function readOrder() {
try {
const product = await Promise.resolve("Pen");
console.log(product);
} catch (error) {
console.error(error);
}
}
readOrder();
console.log("Order requested");
What to expect Order requested prints before Pen, even though the awaited promise is already fulfilled.
How does the browser schedule callbacks?
The event loop coordinates work waiting to run. A ready timer callback runs in a task. A promise reaction is a microtask. After the current synchronous work finishes, the browser processes microtasks before choosing the next task.
How to try it Open a browser tab, then open Developer Tools → Console. Type the example or paste it into the Console after reviewing it, then run it. Reload before repeating declarations with the same names.
console.log("A");
setTimeout(() => console.log("Timer task"), 0);
Promise.resolve().then(() => console.log("Promise microtask"));
console.log("B");
What to expect The order is A, B, Promise microtask, then Timer task.