Skip to content

Instantly share code, notes, and snippets.

View andrewcourtice's full-sized avatar
💭
I may be slow to respond.

Andrew Courtice andrewcourtice

💭
I may be slow to respond.
View GitHub Profile
@andrewcourtice
andrewcourtice / task.ts
Last active April 21, 2024 09:08
Async cancellation using promise extension and abort controller
/*
This a basic implementation of task cancellation using a Promise extension
combined with an AbortController. There are 3 major benefits to this implementation:
1. Because it's just an extension of a Promise the Task is fully
compatible with the async/await syntax.
2. By using the abort controller as a native cancellation token
fetch requests and certain DOM operations can be cancelled inside the task.
3. By passing the controller from parent tasks to new child tasks an entire
async chain can be cancelled using a single AbortController.