Skip to content

Add Promise.Race, Promise.Any, Promise.AllSettled, Cancellation, Timeout, and Finally support#17

Open
JavierusTk wants to merge 4 commits intoLaurensvanrun:mainfrom
JavierusTk:main
Open

Add Promise.Race, Promise.Any, Promise.AllSettled, Cancellation, Timeout, and Finally support#17
JavierusTk wants to merge 4 commits intoLaurensvanrun:mainfrom
JavierusTk:main

Conversation

@JavierusTk
Copy link
Copy Markdown

Summary

This PR extends the Delphi Promises library with several combinators and features commonly found in modern promise implementations (JavaScript/TypeScript):

  • Promise.Race<T> — Resolves/rejects as soon as the first promise settles
  • Promise.Any<T> — Resolves with the first fulfilled promise; rejects only if all fail (raises EAggregatePromiseError)
  • Promise.AllSettled<T> — Waits for all promises to settle, returning results with status (fulfilled/rejected) without short-circuiting on errors
  • Promise.Timeout<T> — Wraps a promise with a deadline; raises EPromiseTimeout if not settled in time
  • Cancellation — Cooperative cancellation via ICancellationToken / TCancellationTokenSource; raises EPromiseCancelled
  • Finally — Executes a callback when the promise settles regardless of outcome (resolved or rejected)

New files

File Purpose
Core/Types/Next.Core.Promises.Cancellation.pas ICancellationToken, TCancellationTokenSource
Core/Types/Next.Core.Promises.Exceptions.pas EPromiseException hierarchy (EPromiseTimeout, EPromiseCancelled, EAggregatePromiseError)
Test/Types/TestPromiseRace.pas 7 test cases for Race
Test/Types/TestPromiseAny.pas 5 test cases for Any
Test/Types/TestPromiseAllSettled.pas 6 test cases for AllSettled
Test/Types/TestPromiseCancellation.pas 8 test cases for Cancellation
Test/Types/TestPromiseTimeout.pas 3 test cases for Timeout
Test/Types/TestPromiseFinally.pas 4 test cases for Finally
Test/Types/TestPromiseExceptions.pas 4 test cases for exception types

Changes to existing files

  • Next.Core.Promises.pas — Added Race, Any, AllSettled, Timeout, Finally, and cancellation-aware execution to TPromise<T>
  • readme.md — Comprehensive documentation for all new features with usage examples
  • TestNext.dpr / TestNext.dproj — Registered new test units

Test plan

  • All 37+ new test cases pass (Race, Any, AllSettled, Cancellation, Timeout, Finally, Exceptions)
  • Existing tests continue to pass
  • Verify on Delphi 10.x+ (developed on Delphi 12)

Design notes

  • Non-breaking: All additions are new methods/types; no existing API signatures were changed
  • Cooperative cancellation: Follows the cancellation token pattern — promises check the token and raise EPromiseCancelled rather than being forcefully
    terminated
  • Exception hierarchy: All new exceptions inherit from EPromiseException for easy catch filtering
  • AllSettled result type: Uses TPromiseResult<T> record with Status (fulfilled/rejected), Value, and ErrorMessage fields

claude and others added 4 commits February 15, 2026 14:55
…Timeout support

Extend the Delphi-Promises library with six new features:

1. Promise.Race<T> - Resolves/rejects with the first settled promise
2. Promise.Any<T> - Resolves with first success, rejects with EAggregateException if all fail
3. Promise.AllSettled<T> - Waits for all promises, returns array of TPromiseSettledResult<T>
4. Cancellation support - ICancellationToken/ICancellationTokenSource with cooperative cancellation
5. .Timeout method - Rejects with ETimeoutException if promise doesn't settle in time
6. Additional .Finally tests - Verifying existing Finally behavior per spec

New files:
- Core/Types/Next.Core.Promises.Exceptions.pas (EAggregateException, ETimeoutException, EOperationCancelled)
- Core/Types/Next.Core.Promises.Cancellation.pas (ICancellationToken, ICancellationTokenSource, TCancellationTokenSource)
- Test/Types/TestPromiseRace.pas (8 tests including stress test)
- Test/Types/TestPromiseAny.pas (7 tests including stress test)
- Test/Types/TestPromiseAllSettled.pas (6 tests including stress test)
- Test/Types/TestPromiseFinally.pas (6 tests)
- Test/Types/TestPromiseCancellation.pas (9 tests)
- Test/Types/TestPromiseTimeout.pas (4 tests)

Modified files:
- Core/Types/Next.Core.Promises.pas (Race, Any, AllSettled, CancelToken, IsCancelled, OnCancelled, Timeout)
- Test/TestNext.dpr (references to new units and test fixtures)

https://claude.ai/code/session_01VeVVLGmQXiukbpfM5p7W99
…ents-euyZO

Add Promise.Race, Promise.Any, Promise.AllSettled, Cancellation, and Timeout support
Replace 6 non-generic test files with generic versions testing across
5 types (Integer, Boolean, String, TSimpleRecord, TMyObject) plus a
new exception type test file. Fix two library bugs discovered during
testing:

- Promise.Race: use dvKeep + manual loser disposal to prevent
  double-free of object-typed values
- Promise.Any: fix variable capture bug in rejection slot tracking;
  use atomic counter instead of captured loop index

Fix variable capture bugs in AllSettled and Race stress tests by
extracting promise creation into helper functions.

750 tests, 0 failures, 0 errors.
…Finally docs

Add comprehensive documentation with code examples, comparison tables
to JavaScript equivalents, and API reference for all new promise
combinators and features.
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.

2 participants