Skip to content

Implement Promise then() logic for pending state and Promise.all/race methods#27

Draft
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-26
Draft

Implement Promise then() logic for pending state and Promise.all/race methods#27
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-26

Conversation

Copy link

Copilot AI commented Aug 12, 2025

This PR implements the missing Promise functionality as requested in the issue:

🔧 Promise.then() for Pending Promises

Previously, when calling .then() on a promise in pending state, the callback was ignored and the method returned undefined. Now promises properly queue callbacks and execute them when resolved/rejected:

let resolveCache;
let promise = new Promise(resolve => {
  resolveCache = resolve;
});

// This now works correctly - callback is queued for later execution
let result = promise.then(value => value + ' processed');
resolveCache('Hello');
// result is now a fulfilled promise with "Hello processed"

🔧 Promise.all() Implementation

Added Promise.all() static method that waits for all input promises to resolve, or rejects immediately if any promise rejects:

let p1 = Promise.resolve('First');
let p2 = Promise.resolve('Second');
let p3 = Promise.resolve('Third');

let allResult = Promise.all([p1, p2, p3]);
// Returns fulfilled promise with ["First", "Second", "Third"]

// Rejects immediately if any promise rejects
let withReject = Promise.all([p1, Promise.reject('error'), p3]);
// Returns rejected promise with "error"

🔧 Promise.race() Implementation

Added Promise.race() static method that resolves/rejects as soon as the first input promise settles:

let race1 = Promise.resolve('Winner');
let race2 = Promise.resolve('Too late');

let raceResult = Promise.race([race1, race2]);
// Returns fulfilled promise with "Winner"

🔧 Proper Promise Resolution Logic

Enhanced the promise resolution to correctly handle when promises are returned from .then() handlers, following ECMAScript specifications:

Promise.resolve('start')
  .then(value => Promise.reject(value + ':reject'))
  .then(
    value => Promise.resolve(value + ':fulfilled'), 
    reason => Promise.resolve(reason + ':handled')
  );
// Correctly chains through promise returns

📋 Changes Made

  • Added callback storage arrays ([[PromiseFulfillReactions]] and [[PromiseRejectReactions]]) to promise objects
  • Modified then() method to queue callbacks for pending promises and return new promises
  • Updated resolve() and reject() functions to execute stored callback reactions
  • Implemented proper promise resolution logic for returned promises from handlers
  • Added Promise.all() and Promise.race() static methods with full ECMAScript compliance
  • Added comprehensive test coverage for all new functionality

All existing promise tests continue to pass, ensuring no regression in functionality.

Fixes #26.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Author

Copilot AI commented Aug 12, 2025

@echosoar 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 2 commits August 12, 2025 10:03
…e (partial)

Co-authored-by: echosoar <14832743+echosoar@users.noreply.github.com>
Co-authored-by: echosoar <14832743+echosoar@users.noreply.github.com>
Copilot AI changed the title [WIP] 实现 Promise 的 then 逻辑 Implement Promise then() logic for pending state and Promise.all/race methods Aug 12, 2025
Copilot AI requested a review from echosoar August 12, 2025 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

实现 Promise 的 then 逻辑

2 participants