we (web engine): Experimental web browser project to understand the limits of Claude

JS Promise and microtask queue #102

open opened by pierrelf.com

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 Promise
  • Promise.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 promise
  • Promise.all(iterable) — fulfill when all fulfill, reject on first rejection
  • Promise.allSettled(iterable) — wait for all to settle
  • Promise.race(iterable) — settle with first settlement
  • Promise.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.

sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mhn3oxz6dq2k