{"id":16419134,"url":"https://github.com/cameronhunter/pending-promises","last_synced_at":"2025-10-14T04:04:02.718Z","repository":{"id":207997240,"uuid":"720590556","full_name":"cameronhunter/pending-promises","owner":"cameronhunter","description":"A map of pending promises that can be resolved/rejected at a later time.","archived":false,"fork":false,"pushed_at":"2024-02-19T01:54:47.000Z","size":335,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-07T05:49:33.462Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/cameronhunter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-19T00:12:14.000Z","updated_at":"2023-11-19T22:46:53.000Z","dependencies_parsed_at":"2023-11-19T01:26:22.155Z","dependency_job_id":"640f0e7f-e3ea-4f6e-a985-ad48ddfdba8f","html_url":"https://github.com/cameronhunter/pending-promises","commit_stats":null,"previous_names":["cameronhunter/pending-promises"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fpending-promises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fpending-promises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fpending-promises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cameronhunter%2Fpending-promises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cameronhunter","download_url":"https://codeload.github.com/cameronhunter/pending-promises/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240486260,"owners_count":19809146,"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-10-11T07:16:02.292Z","updated_at":"2025-10-14T04:03:57.686Z","avatar_url":"https://github.com/cameronhunter.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `@cameronhunter/pending-promises`\n\n[![npm package](https://img.shields.io/npm/v/%40cameronhunter/pending-promises?logo=npm)](https://www.npmjs.com/package/@cameronhunter/pending-promises)\n[![npm downloads](https://img.shields.io/npm/dm/%40cameronhunter/pending-promises?logo=npm)](https://www.npmjs.com/package/@cameronhunter/pending-promises)\n[![main branch status](https://img.shields.io/github/actions/workflow/status/cameronhunter/pending-promises/post-merge.yml?logo=github\u0026label=main)](https://github.com/cameronhunter/pending-promises/actions/workflows/post-merge.yml)\n\n\u003e A map of pending promises that can be resolved/rejected at a later time.\n\nThis data structure helps convert event-based APIs to promise-based. Here's an\nexample that wraps a WebSocket client, providing a promise-based API.\n\n```ts\nimport { PendingPromises } from '@cameronhunter/pending-promises';\n\nclass MyAPI {\n    readonly #responses: PendingPromises = new PendingPromises();\n    readonly #ws: WebSocket;\n\n    constructor(ws: WebSocket) {\n        this.#ws = ws;\n        this.#ws.on('message', this.#onMessage.bind(this));\n    }\n\n    send(message: string): Promise\u003cstring\u003e {\n        // Create a new pending promise\n        const [id, promise] = this.#responses.create\u003cstring\u003e();\n\n        ws.send(JSON.stringify({ id, message }), (err) =\u003e {\n            if (err) {\n                // Reject immediately if sending fails.\n                this.#responses.reject(id, err);\n            }\n        });\n\n        return promise;\n    }\n\n    #onMessage(event: WebSocket.MessageEvent): void {\n        const { id, result, error } = JSON.parse(event.data as string);\n\n        if (id) {\n            if (error) {\n                this.#responses.reject(id, error);\n            } else {\n                this.#responses.resolve(id, result);\n            }\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcameronhunter%2Fpending-promises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcameronhunter%2Fpending-promises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcameronhunter%2Fpending-promises/lists"}