Skip to content

feat: Make sleep cancellable#72

Merged
mykola-mokhnach merged 4 commits into
masterfrom
cancel
May 10, 2026
Merged

feat: Make sleep cancellable#72
mykola-mokhnach merged 4 commits into
masterfrom
cancel

Conversation

@mykola-mokhnach
Copy link
Copy Markdown
Contributor

@mykola-mokhnach mykola-mokhnach commented May 9, 2026

Summary
Replaces the old fire-and-forget sleep with a cancellable delay: the returned promise includes cancel(), which clears the timer and either rejects with PromiseCancellationError (configurable), resolves when cancelError: null, or uses string / Error overrides like withTimeout.

API

  • sleep(ms) or sleep({ ms, cancelError? }) — runtime checks: finite number, or plain object with finite ms (includes Object.create(null)).
  • CancellablePromise<T> — generic Promise<T> & { cancel(): void } (default T is void; sleep returns CancellablePromise<void>).
  • SleepArg, SleepOptions, PromiseCancellationError exported from the package surface.

Tests
Coverage for timing, cancel/reject/resolve paths, cancelError variants, invalid arguments, and plain-object edge cases.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a CancellablePromise<void> that supports .cancel() and configurable cancellation behavior.
  • Add exported types (SleepOptions, SleepArg, CancellablePromise) and a PromiseCancellation error, plus argument parsing/runtime validation for sleep.
  • 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.

Comment thread lib/types.ts Outdated
Comment thread lib/types.ts
Comment thread lib/asyncbox.ts Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Comment thread lib/asyncbox.ts Outdated
timeoutId = undefined;
}
if (cancelError === null) {
resolveFn?.();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment thread lib/asyncbox.ts
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';
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@mykola-mokhnach mykola-mokhnach merged commit 8e2ff41 into master May 10, 2026
5 checks passed
@mykola-mokhnach mykola-mokhnach deleted the cancel branch May 10, 2026 18:27
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))
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 6.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants