{"id":19206865,"url":"https://github.com/html-next/cancellation-token","last_synced_at":"2025-05-12T17:27:46.539Z","repository":{"id":66281336,"uuid":"72216631","full_name":"html-next/cancellation-token","owner":"html-next","description":"JS Cancellation Tokens for composable async cleanup in ES2015","archived":false,"fork":false,"pushed_at":"2018-01-18T20:28:48.000Z","size":3,"stargazers_count":4,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-06T16:41:49.236Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/html-next.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2016-10-28T14:55:09.000Z","updated_at":"2022-04-13T11:41:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"9d0590b8-b38e-4fc2-8c08-30fa7c8d0090","html_url":"https://github.com/html-next/cancellation-token","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"4325e4d7a157b9146f05d68ccbb688087d7f7bcd"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/html-next%2Fcancellation-token","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/html-next%2Fcancellation-token/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/html-next%2Fcancellation-token/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/html-next%2Fcancellation-token/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/html-next","download_url":"https://codeload.github.com/html-next/cancellation-token/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253506591,"owners_count":21919069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T13:17:14.800Z","updated_at":"2025-05-12T17:27:46.492Z","avatar_url":"https://github.com/html-next.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cancellation Token\n\nES2015 JS Cancellation Tokens for composable async cleanup\n\n## Installation\n\n```cli\nyarn add cancellation-token\n```\n\n## About\n\nTokens are extremely lightweight and easy to grok, their power lies in\ntheir composability. The entire implementation is this:\n\n```js\nexport default class Token {\n  constructor(parent) {\n    this._parent = parent;\n    this._cancelled = false;\n  }\n\n  get cancelled() {\n    return this._cancelled || (this._cancelled = this._parent ? this._parent.cancelled : false);\n  }\n\n  cancel() {\n    this._cancelled = true;\n  }\n}\n```\n\n## Usage\n\nEach example builds on the previous in order.\n\n**create**\n```js\nimport Token from 'cancellation-token';\n\nlet token = new Token();\n```\n\n**conditional work**\n```js\nlet jobs = [];\n\nfunction cancellableJob(job, token) {\n  return function() {\n    if (!token.cancelled) {\n      job();\n    }\n  };\n}\n\nfunction schedule(job) {\n  let token = new Token();\n  \n  jobs.push(cancellableJob(job, token);\n  \n  return token;\n}\n```\n\n**cancelling work**\n```js\nlet scheduledJob = schedule(() =\u003e { alert(\"hello world!\"); });\nscheduledJob.cancel();\n```\n\n**inheritance**\n```js\nlet grandParentToken = new Token();\nlet parentToken = new Token(grandParentToken);\n\nfunction scheduleWithParent(job, parentToken) {\n  let token = new Token(parentToken);\n  \n  jobs.push(cancellableJob(job, token);\n  \n  return token;\n}\n\n// cancel the task and all descendants\ngrandParentToken.cancel();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhtml-next%2Fcancellation-token","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhtml-next%2Fcancellation-token","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhtml-next%2Fcancellation-token/lists"}