How Does JavaScript’s Promise Work Under the Hood? A Deep Dive

This article walks through the inner mechanics of JavaScript Promises, explaining their basic prototype, asynchronous handling with the event loop, state management, chainable behavior, and error handling, all illustrated with step‑by‑step code examples and diagrams.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
How Does JavaScript’s Promise Work Under the Hood? A Deep Dive

Introduction

The author revisits a previously written article on basic Promise usage and now delves deeper into how the Promise mechanism is implemented according to the Promise/A+ specification, targeting readers already familiar with Promise basics.

Minimal Promise Prototype

A simple skeleton of a Promise is presented, showing how then registers callbacks into a queue and how the constructor receives a resolve function that later executes those callbacks.

Adding Asynchronous Execution

To ensure callbacks are registered before resolve runs, the implementation wraps the callback execution in setTimeout, pushing it to the end of the JavaScript task queue.

The author notes that simply using setTimeout still leaves a problem: callbacks added after the asynchronous operation has already succeeded would never run.

State Management

To solve this, a state machine with pending , fulfilled , and rejected is introduced, mirroring the Promise/A+ spec that a promise can transition from pending to either fulfilled or rejected exactly once.

Chainable Promises

The article explains how returning a new Promise from then enables chaining, and how the internal handle method links the current promise with the next one by pushing both the fulfillment and rejection callbacks into the queue.

A detailed step‑by‑step walkthrough of a multi‑promise example (referred to as example 4) shows how each then creates a bridge promise, how callbacks are propagated, and how the final result is produced.

Error Handling

When an asynchronous operation fails, the promise transitions to rejected and executes registered failure callbacks. The implementation adds a reject method and a shared execute helper to handle both fulfillment and rejection.

Errors that occur inside callbacks are caught with try‑catch, converting the bridge promise to a rejected state, and the code ensures that multiple calls to resolve or reject are ignored after the first transition.

Conclusion

The author summarizes that understanding Promise internals boils down to recognizing that then merely registers callbacks while resolve triggers their execution, and that the whole mechanism follows the observer pattern.

References

In‑depth Promise analysis: http://coderlt.coding.me/2016/12/04/promise-in-depth-an-introduction-2/

JavaScript Promises … In Wicked Detail: http://www.mattgreer.org/articles/promises-in-wicked-detail/

Original article (Chinese): https://mengera88.github.io/2017/05/18/Promise%E5%8E%9F%E7%90%86%E8%A7%A3%E6%9E%90/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsJavaScriptAsynchronousPromise
Tencent IMWeb Frontend Team
Written by

Tencent IMWeb Frontend Team

IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.