Add Promise.Race, Promise.Any, Promise.AllSettled, Cancellation, Timeout, and Finally support#17
Open
JavierusTk wants to merge 4 commits intoLaurensvanrun:mainfrom
Open
Add Promise.Race, Promise.Any, Promise.AllSettled, Cancellation, Timeout, and Finally support#17JavierusTk wants to merge 4 commits intoLaurensvanrun:mainfrom
JavierusTk wants to merge 4 commits intoLaurensvanrun:mainfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 settlesPromise.Any<T>— Resolves with the first fulfilled promise; rejects only if all fail (raisesEAggregatePromiseError)Promise.AllSettled<T>— Waits for all promises to settle, returning results with status (fulfilled/rejected) without short-circuiting on errorsPromise.Timeout<T>— Wraps a promise with a deadline; raisesEPromiseTimeoutif not settled in timeCancellation— Cooperative cancellation viaICancellationToken/TCancellationTokenSource; raisesEPromiseCancelledFinally— Executes a callback when the promise settles regardless of outcome (resolved or rejected)New files
Core/Types/Next.Core.Promises.Cancellation.pasICancellationToken,TCancellationTokenSourceCore/Types/Next.Core.Promises.Exceptions.pasEPromiseExceptionhierarchy (EPromiseTimeout,EPromiseCancelled,EAggregatePromiseError)Test/Types/TestPromiseRace.pasTest/Types/TestPromiseAny.pasTest/Types/TestPromiseAllSettled.pasTest/Types/TestPromiseCancellation.pasTest/Types/TestPromiseTimeout.pasTest/Types/TestPromiseFinally.pasTest/Types/TestPromiseExceptions.pasChanges to existing files
Next.Core.Promises.pas— AddedRace,Any,AllSettled,Timeout,Finally, and cancellation-aware execution toTPromise<T>readme.md— Comprehensive documentation for all new features with usage examplesTestNext.dpr/TestNext.dproj— Registered new test unitsTest plan
Design notes
EPromiseCancelledrather than being forcefullyterminated
EPromiseExceptionfor easy catch filteringAllSettledresult type: UsesTPromiseResult<T>record withStatus(fulfilled/rejected),Value, andErrorMessagefields