{"id":41419125,"url":"https://github.com/baileyherbert/promises","last_synced_at":"2026-01-23T14:00:03.641Z","repository":{"id":57100155,"uuid":"436997339","full_name":"baileyherbert/promises","owner":"baileyherbert","description":"A small collection of utilities for JavaScript promises.","archived":false,"fork":false,"pushed_at":"2021-12-14T02:25:14.000Z","size":86,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-30T21:43:46.672Z","etag":null,"topics":["completion-source","promises","timeouts"],"latest_commit_sha":null,"homepage":"https://npmjs.com/@baileyherbert/promises","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/baileyherbert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-10T13:57:35.000Z","updated_at":"2021-12-14T02:25:17.000Z","dependencies_parsed_at":"2022-08-22T23:10:47.276Z","dependency_job_id":null,"html_url":"https://github.com/baileyherbert/promises","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/baileyherbert/promises","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baileyherbert%2Fpromises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baileyherbert%2Fpromises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baileyherbert%2Fpromises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baileyherbert%2Fpromises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/baileyherbert","download_url":"https://codeload.github.com/baileyherbert/promises/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/baileyherbert%2Fpromises/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28693434,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T11:01:27.039Z","status":"ssl_error","status_checked_at":"2026-01-23T11:00:26.909Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["completion-source","promises","timeouts"],"created_at":"2026-01-23T14:00:02.159Z","updated_at":"2026-01-23T14:00:03.628Z","avatar_url":"https://github.com/baileyherbert.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Promises\n\nThis is a small collection of promise-based utilities.\n\n```\nnpm install @baileyherbert/promises\n```\n\n## Documentation\n\n### `PromiseCompletionSource`\n\nThis class allows you to create a `Promise` which can easily be resolved or rejected on demand from the outside.\n\n```ts\nimport { PromiseCompletionSource } from '@baileyherbert/promises';\n\nfunction runFakeTask() {\n    const source = new PromiseCompletionSource();\n\n    // Resolves the promise after 5 seconds\n    setTimeout(() =\u003e {\n        source.resolve();\n    }, 5000);\n\n    // Returns the promise object\n    return source.promise;\n}\n\n// Resolves after 5 seconds\nawait runFakeTask();\n```\n\n### `PromiseTimeoutSource`\n\nThis class creates a promise that resolves to a boolean after the specified time, but can be cancelled prematurely. The boolean is `true` if the timeout was triggered, or `false` if cancelled.\n\nYou can also specify a custom `action` to execute when the timeout is reached.\n\n```ts\nimport { PromiseTimeoutSource } from '@baileyherbert/promises';\n```\n\n**Example 1:** Wait for 30 seconds\n\n```ts\nawait new PromiseTimeoutSource(30000);\n```\n\n**Example 2:** Run a task after 30 seconds\n\n```ts\nnew PromiseTimeoutSource(30000, () =\u003e {\n    console.log('This runs after 30 seconds!');\n});\n```\n\n**Example 3:** Cancel a task before it's scheduled to run\n\n```ts\nconst timeout = new PromiseTimeoutSource(30000, () =\u003e {\n    console.log('This runs after 30 seconds!');\n});\n\n// The action will never run because it gets cancelled after 15 sec!\nsetTimeout(() =\u003e timeout.cancel(), 15000);\n\n// Confirm that it was cancelled\nconst result = await timeout;\nconsole.log('The timeout was', result ? 'fulfilled' : 'cancelled');\n```\n\nYou could use this to cancel and clean up an operation after a specified amount of time, but stop the cancellation\ntask from running if it completes in time.\n\n### `Task`\n\nThis is a promise alternative that must be manually started, and can be cancelled. It's very similar to C#'s tasks.\n\n```ts\nimport { Task } from '@baileyherbert/promises';\n\nconst task = new Task((resolve, reject) =\u003e {\n    resolve();\n});\n\n// Tell the task when to start\ntask.start();\n\n// Wait for the task to finish\nawait task;\n\n// You can also wait with a timeout\nif (!await task.wait(10000)) {\n    // Timed out\n}\n\n// Get the resolved value\nif (task.isResolved) {\n    const taskResult = task.result;\n}\n```\n\nYou can combine this with a `CancellationToken` to make the task cancellable.\n\n```ts\nconst source = new CancellationTokenSource();\nconst token = source.token;\nconst task = new Task(async (resolve, reject, cancel) =\u003e {\n    for (let i = 0; i \u003c 100; i++) {\n        await Task.delay(100);\n        cancel.throwIfCancellationRequested();\n    }\n}, token);\n\nif (await task === TaskStatus.Cancelled) {\n    // It was cancelled!\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaileyherbert%2Fpromises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbaileyherbert%2Fpromises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbaileyherbert%2Fpromises/lists"}