{"id":22618560,"url":"https://github.com/dokmic/ts-async-decorators","last_synced_at":"2025-08-18T10:16:07.382Z","repository":{"id":57380491,"uuid":"328238909","full_name":"dokmic/ts-async-decorators","owner":"dokmic","description":"TypeScript Async Method Decorators","archived":false,"fork":false,"pushed_at":"2021-10-31T09:11:07.000Z","size":47,"stargazers_count":20,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-12T01:55:08.316Z","etag":null,"topics":["async","cancelable","debounce","decorator","decorators","mutex","promise","retry","semaphore","throttle","timeout","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ts-async-decorators","language":"TypeScript","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/dokmic.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}},"created_at":"2021-01-09T20:18:54.000Z","updated_at":"2024-12-03T18:19:16.000Z","dependencies_parsed_at":"2022-09-05T14:31:00.561Z","dependency_job_id":null,"html_url":"https://github.com/dokmic/ts-async-decorators","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/dokmic/ts-async-decorators","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dokmic%2Fts-async-decorators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dokmic%2Fts-async-decorators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dokmic%2Fts-async-decorators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dokmic%2Fts-async-decorators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dokmic","download_url":"https://codeload.github.com/dokmic/ts-async-decorators/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dokmic%2Fts-async-decorators/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270149333,"owners_count":24535726,"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","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["async","cancelable","debounce","decorator","decorators","mutex","promise","retry","semaphore","throttle","timeout","typescript"],"created_at":"2024-12-08T21:08:23.426Z","updated_at":"2025-08-18T10:16:07.354Z","avatar_url":"https://github.com/dokmic.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TypeScript Async Method Decorators 🧰\n[![NPM](https://img.shields.io/npm/v/ts-async-decorators.svg)](https://www.npmjs.com/package/ts-async-decorators)\n[![Downloads](https://img.shields.io/npm/dm/ts-async-decorators)](https://www.npmjs.com/package/ts-async-decorators)\n[![Tests](https://github.com/dokmic/ts-async-decorators/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/dokmic/ts-async-decorators/actions/workflows/tests.yaml)\n[![Code Coverage](https://codecov.io/gh/dokmic/ts-async-decorators/badge.svg?branch=master)](https://codecov.io/gh/dokmic/ts-async-decorators?branch=master)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nThis package provides a collection of asynchronous method decorators with an elegant declarative API.\n\n## What's Inside?\n- `after` - post action.\n- `before` - pre action.\n- `cancelable` - canceling execution.\n- `debounce` - delaying execution by timeout since the last call.\n- `retry` - retrying on fail.\n- `semaphore` - limiting the number of simultaneous calls.\n- `throttle` - limiting the number of calls per time range.\n- `timeout` - limiting the execution time.\n\n## Get Started\n```bash\nnpm install --save ts-async-decorators\n```\n\n## API\n### `after`\nCalls an action after a decorated method.\n\n```typescript\nafter({ action, wait = false }): Decorator\n```\n- `action: (instance) =\u003e unknown` - The action to call after the decorated method.\n- `wait: boolean` - The flag to wait with promise resolution until the action completes.\n\n#### Example\n```typescript\nimport { after } from 'ts-async-decorators';\n\nclass SomeClass {\n  @after({ action() { console.log('called fetch!'); } })\n  fetch() {\n    // ...\n  }\n}\n```\n\n### `before`\nCalls an action before a decorated method.\n\n```typescript\nbefore({ action, wait = false }): Decorator\n```\n- `action: (instance) =\u003e unknown` - The action to call before the decorated method.\n- `wait: boolean` - The flag to wait with the decorated method call until the action completes.\n\n#### Example\n```typescript\nimport { before } from 'ts-async-decorators';\n\nclass SomeClass {\n  @before({ action() { console.log('calling fetch!'); } })\n  fetch() {\n    // ...\n  }\n}\n```\n\n### `cancelable`\nWraps a call inside a cancelable promise.\n\n```typescript\ncancelable({ onCancel = undefined }): Decorator\n```\n- `onCancel: (instance) =\u003e void` - The action to call on canceling the returned promise.\n\nThere is also an option to set the cancelation callback from within the decorated method.\n\n```typescript\nonCancel(callback): void\n```\n- `callback: (instance) =\u003e void` - The callback that will be called on promise cancelation.\n\n#### Example using parameters\n```typescript\nimport { cancelable } from 'ts-async-decorators';\n\nclass SomeClass {\n  @cancelable({ onCancel() { this.stop(); } })\n  start() {\n    // ...\n  }\n\n  stop() {\n    // ...\n  }\n}\n```\n\n#### Example using `onCancel`\n```typescript\nimport { cancelable, onCancel } from 'ts-async-decorators';\n\nclass SomeClass {\n  @cancelable()\n  fetch() {\n    const controller = new AbortController();\n    const { signal } = controller;\n    onCancel(() =\u003e controller.abort());\n\n    return fetch('http://example.com', { signal });\n  }\n}\n```\n\n\n### `debounce`\nDelays execution by timeout since the last call.\n\n```typescript\ndebounce({ immediate = false, timeout }): Decorator\n```\n- `immediate: boolean` - The flag to call the decorated method on the leading edge.\n- `timeout: number | ((instance) =\u003e number)` - The decorated method will be called only once after `timeout` milliseconds since the last call.\n\n#### Example\n```typescript\nimport { debounce } from 'ts-async-decorators';\n\nclass SomeClass {\n  @debounce({ timeout: 2000 })\n  handleChange() {\n    // ...\n  }\n}\n```\n\n### `retry`\nRetries failed method calls.\n\n```typescript\nretry({ retries }): Decorator\n```\n- `retries: number | ((instance) =\u003e number)` - The retries number.\n\n#### Example\n```typescript\nimport { retry } from 'ts-async-decorators';\n\nclass SomeClass {\n  @retry({ retries: 3 })\n  fetch() {\n    // ...\n  }\n}\n```\n\n### `semaphore`\nLimits the number of simultaneous calls.\n\n```typescript\nsemaphore({ limit }): Decorator\n```\n- `limit: number` - The max number of simultaneous calls.\n\n#### Example\n```typescript\nimport { semaphore } from 'ts-async-decorators';\n\nconst mutex = () =\u003e semaphore({ limit: 1 });\n\nclass SomeClass {\n  @mutex()\n  connect() {\n    // ...\n  }\n\n  @semaphore({ limit: 2 })\n  read() {\n    // ...\n  }\n}\n```\n\n### `throttle`\nLimits the number of calls per time range.\n\n```typescript\nthrottle({ immediate = false, timeout }): Decorator\n```\n- `immediate: boolean` - The flag to call the decorated method on the leading edge.\n- `timeout: number | ((instance) =\u003e number)` - The decorated method will be called only once within `timeout` milliseconds.\n\n#### Example\n```typescript\nimport { throttle } from 'ts-async-decorators';\n\nclass SomeClass {\n  @throttle({ timeout: 1000 })\n  handleResize() {\n    // ...\n  }\n}\n```\n\n### `timeout`\nLimits method execution time.\n\n```typescript\ntimeout({ timeout, reason = 'Operation timeout.' }): Decorator\n```\n- `timeout: number | ((instance) =\u003e number)` - The execution timeout in milliseconds.\n- `reason: string` - The timeout reason.\n\n#### Example\n```typescript\nimport { cancelable, timeout, onCancel } from 'ts-async-decorators';\n\nclass SomeClass {\n  @timeout({ timeout: 10000, reason = 'Fetch timeout.' })\n  @cancelable()\n  fetch() {\n    const controller = new AbortController();\n    const { signal } = controller;\n    onCancel(() =\u003e controller.abort());\n\n    return fetch('http://example.com', { signal });\n  }\n}\n```\n\n## Examples\n- [Bluetooth Low-Energy Peripheral Device](https://github.com/dokmic/bluetooth-device).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdokmic%2Fts-async-decorators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdokmic%2Fts-async-decorators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdokmic%2Fts-async-decorators/lists"}