Implement the Promise built-in and the microtask queue for asynchronous execution.
Scope#
Promise#
Promise(executor)constructor — executor receives (resolve, reject)- Promise states: pending, fulfilled, rejected
Promise.prototype.then(onFulfilled, onRejected)— returns new PromisePromise.prototype.catch(onRejected)— sugar for then(undefined, onRejected)Promise.prototype.finally(onFinally)— runs callback regardless of outcome- Promise resolution procedure (handle thenables)
Promise.resolve(value)— wrap value in fulfilled promise (or return if already promise)Promise.reject(reason)— return rejected promisePromise.all(iterable)— fulfill when all fulfill, reject on first rejectionPromise.allSettled(iterable)— wait for all to settlePromise.race(iterable)— settle with first settlementPromise.any(iterable)— fulfill with first fulfillment, reject with AggregateError if all reject
Microtask Queue#
- Job queue for promise reactions (then/catch/finally callbacks)
- Microtasks run to completion after each macrotask
- Ordering: microtasks drain fully before next macrotask
- Integration point with VM event loop (prepare for setTimeout in Phase 11)
Execution Model#
- Promise reactions enqueue microtasks (not executed synchronously)
- Chained promises propagate values/errors through the chain
- Unhandled rejection tracking (basic — log or flag)
Acceptance Criteria#
- Promise constructor calls executor synchronously
- then/catch/finally return new Promises with correct chaining
- Promise.resolve/reject create correctly-stated promises
- Promise.all fulfills with array of results
- Promise.all rejects on first rejection
- Promise.race settles with first settlement
- Promise.allSettled waits for all
- Promise.any rejects with AggregateError
- Microtask queue drains after synchronous code
- Chained promises propagate values correctly
- Unit tests for all Promise methods and chaining scenarios
Phase 10 — JavaScript Engine (issue 13 of 15). Depends on: JS core built-ins.