feat: Make sleep cancellable#72
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates sleep from a simple delay to a cancellable delay primitive, exposing a cancel() method on the returned promise and adding supporting exported types and tests. It also tightens the TypeScript configuration to strict: true and adds missing test type dependencies.
Changes:
- Replace
sleep(ms)with an overload-based API returning aCancellablePromise<void>that supports.cancel()and configurable cancellation behavior. - Add exported types (
SleepOptions,SleepArg,CancellablePromise) and aPromiseCancellationerror, plus argument parsing/runtime validation forsleep. - Expand test coverage for cancellation paths, argument validation, and plain-object edge cases; add
@types/chai/@types/chai-as-promised.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tsconfig.json | Enables strict type-checking for the project. |
| lib/types.ts | Adds new exported types for cancellable sleep (SleepOptions, SleepArg, CancellablePromise). |
| lib/asyncbox.ts | Implements cancellable sleep, adds PromiseCancellation, and re-exports the new types. |
| test/asyncbox-specs.ts | Adds tests for cancel/resolve/reject behavior and invalid argument handling. |
| package.json | Adds TypeScript type packages for chai and chai-as-promised. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
eglitise
reviewed
May 9, 2026
| timeoutId = undefined; | ||
| } | ||
| if (cancelError === null) { | ||
| resolveFn?.(); |
Comment on lines
+21
to
+27
| /** Thrown when a promise is cancelled via `cancel`. */ | ||
| export class PromiseCancellationError extends Error { | ||
| constructor(message: string = 'Promise cancelled') { | ||
| super(message); | ||
| this.name = 'PromiseCancellationError'; | ||
| } | ||
| } |
KazuCocoa
approved these changes
May 10, 2026
github-actions Bot
pushed a commit
that referenced
this pull request
May 10, 2026
## [6.3.0](v6.2.0...v6.3.0) (2026-05-10) ### Features * Make sleep cancellable ([#72](#72)) ([8e2ff41](8e2ff41))
|
🎉 This PR is included in version 6.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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
Replaces the old fire-and-forget
sleepwith a cancellable delay: the returned promise includescancel(), which clears the timer and either rejects withPromiseCancellationError(configurable), resolves whencancelError: null, or uses string /Erroroverrides likewithTimeout.API
sleep(ms)orsleep({ ms, cancelError? })— runtime checks: finitenumber, or plain object with finitems(includesObject.create(null)).CancellablePromise<T>— genericPromise<T> & { cancel(): void }(defaultTisvoid;sleepreturnsCancellablePromise<void>).SleepArg,SleepOptions,PromiseCancellationErrorexported from the package surface.Tests
Coverage for timing, cancel/reject/resolve paths,
cancelErrorvariants, invalid arguments, and plain-object edge cases.