javascript
55 lines · 10 steps
Building a Promise from scratch
A minimal Promise implementation showing how state, microtasks, and chaining fit together.
Explained by
highlit
1const PENDING = 'pending';
2const FULFILLED = 'fulfilled';
3const REJECTED = 'rejected';
4
5class MyPromise {
6 constructor(executor) {
7 this.state = PENDING;
8 this.value = undefined;
9 this.callbacks = [];
10 const resolve = (value) => this.settle(FULFILLED, value);
11 const reject = (reason) => this.settle(REJECTED, reason);
12 try {
13 executor(resolve, reject);
14 } catch (err) {
15 reject(err);
16 }
17 }
18
19 settle(state, value) {
20 if (this.state !== PENDING) return;
21 this.state = state;
22 this.value = value;
23 this.callbacks.forEach((cb) => this.schedule(cb));
24 this.callbacks = [];
25 }
26
27 schedule(cb) {
28 queueMicrotask(() => cb(this.state, this.value));
29 }
30
31 then(onFulfilled, onRejected) {
32 return new MyPromise((resolve, reject) => {
33 const handle = (state, value) => {
34 const handler = state === FULFILLED ? onFulfilled : onRejected;
35 if (typeof handler !== 'function') {
36 state === FULFILLED ? resolve(value) : reject(value);
37 return;
38 }
39 try {
40 const result = handler(value);
41 if (result instanceof MyPromise) result.then(resolve, reject);
42 else resolve(result);
43 } catch (err) {
44 reject(err);
45 }
46 };
47 if (this.state === PENDING) this.callbacks.push(handle);
48 else this.schedule(handle);
49 });
50 }
51
52 catch(onRejected) {
53 return this.then(undefined, onRejected);
54 }
55}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A promise is a small state machine that transitions from pending to a single terminal state exactly once.
- 2Callbacks registered before settlement are queued, and resolution always runs asynchronously via the microtask queue.
- 3then returns a new promise whose fate is decided by the handler's return value, enabling chains and flattening nested promises.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
javascript
import { useState, useEffect, useCallback, useRef } from "react"; export function usePersistentForm(storageKey, initialValues) { const [values, setValues] = useState(() => {
A React hook that persists form state to localStorage
custom-hooks
localstorage
debounce
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/building-a-promise-from-scratch-explained-javascript-bdfe/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.