Do promises resolve asynchronously or synchronously?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
A common question among programmers, especially those new to JavaScript, is whether promises resolve asynchronously or synchronously. Understanding this aspect of promises is crucial for being able to write efficient and effective asynchronous code. Let's dive into the technical details, with examples to clarify.
Understanding Promises
Promises are a popular way to handle asynchronous operations in JavaScript, allowing developers to write cleaner and more manageable asynchronous code. A promise represents a value that may be available now, or in the future, or never.
Promise States
A promise can be in one of three states:
- Pending: The initial state. The operation has not completed yet.
- Fulfilled: The operation completed successfully.
- Rejected: The operation failed.
Promises provide a then
method, which allows us to specify what to do when the promise is fulfilled or rejected.
Asynchronous or Synchronous?
To answer the core question: Promises always resolve asynchronously, even though they may execute synchronously in terms of the initial execution context. Let's break this down:
Task Queue and Microtasks
JavaScript uses an event loop, which continuously checks a queue of tasks to execute. These tasks are divided into:
- Macrotasks: These include things like I/O events,
setTimeout,setInterval, etc. - Microtasks: These are executed immediately after the current operation completes, and they include promise callbacks,
process.nextTick, andMutationObserver.
Promise callbacks (then
, catch
, finally
) are placed in the microtask queue. This means that even if the promise is resolved immediately, the callbacks are executed in the next event loop cycle—after the current function execution completes.
Example: Synchronous vs. Asynchronous
- "Start" and "Promise initialized" are logged immediately.
- "End" is logged next because promise resolution (although immediate) pushes the
thencallback to the microtask queue. - Finally, "Promise fulfilled with: Resolved value" is logged after the current stack is clear.
- Non-blocking: Promises are non-blocking by nature. This means they are designed to prevent operations from halting execution while waiting for a promise to complete.
- Callback Execution: Since the
thenmethod attaches callbacks that are executed asynchronously (after the microtask queue), promises don't block the code that follows.

