{"id":22404777,"url":"https://github.com/jdeurt/unwrapped-promise","last_synced_at":"2025-03-27T01:17:30.387Z","repository":{"id":61009855,"uuid":"547579224","full_name":"jdeurt/unwrapped-promise","owner":"jdeurt","description":"An extension of Javascript's native Promise class that gives you more control over its lifecycle","archived":false,"fork":false,"pushed_at":"2023-09-06T23:41:11.000Z","size":49,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T06:42:49.078Z","etag":null,"topics":["javascript","promise"],"latest_commit_sha":null,"homepage":"","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/jdeurt.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-07T23:32:34.000Z","updated_at":"2023-05-14T03:57:30.000Z","dependencies_parsed_at":"2023-01-19T23:46:20.662Z","dependency_job_id":null,"html_url":"https://github.com/jdeurt/unwrapped-promise","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":"jdeurt/ts-module-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeurt%2Funwrapped-promise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeurt%2Funwrapped-promise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeurt%2Funwrapped-promise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdeurt%2Funwrapped-promise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdeurt","download_url":"https://codeload.github.com/jdeurt/unwrapped-promise/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245761302,"owners_count":20667895,"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":["javascript","promise"],"created_at":"2024-12-05T10:15:09.084Z","updated_at":"2025-03-27T01:17:30.347Z","avatar_url":"https://github.com/jdeurt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# unwrapped-promise\n\n[![Version](https://img.shields.io/npm/v/unwrapped-promise.svg)](https://www.npmjs.com/package/unwrapped-promise)\n![Prerequisite](https://img.shields.io/badge/node-%3E%3D16-blue.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](#)\n\n\u003e An extension of Javascript's native Promise class that gives you more control over its lifecycle\n\n## Table of contents\n\n-   [Intstallation](#installation)\n-   [Usage](#usage)\n    -   [Reading promise status](#reading-promise-status)\n    -   [Forcing rejection (aborting promises)](#forcing-rejection-aborting-promises)\n    -   [Flat promise behavior](#flat-promise-behavior)\n    -   [Waiting for settlement](#waiting-for-settlement)\n    -   [Creating an `UnwrappedPromise` from a regular `Promise`](#creating-an-unwrappedpromise-from-a-regular-promise)\n    -   [\"Re-wrapping\" a promise](#re-wrapping-a-promise)\n    -   [Resolving from a callback](#resolving-from-a-callback)\n    -   [Timer utilities](#timer-utilities)\n-   [Examples](#examples)\n-   [License](#license)\n\n## Installation\n\n### NPM\n\n```bash\nnpm i unwrapped-promise\n```\n\n### Yarn\n\n```bash\nyarn i unwrapped-promise\n```\n\n## Usage\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst promise = new UnwrappedPromise((resolve, reject) =\u003e {\n    // ...\n});\n```\n\nAn unwrapped promise is an extension of native promises. Unwrapped promises expose some methods and properties that give you more control the promise's lifecycle. They also come with some nifty utility functions that make common promise patterns easier to work with.\n\n### Reading promise status\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst promise = new UnwrappedPromise((resolve) =\u003e setTimeout(resolve, 0));\n\nconsole.log(promise.status); // \"pending\"\n\nawait promise;\n\nconsole.log(promise.status); // \"resolved\"\n```\n\n### Forcing rejection (aborting promises)\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst promise = new UnwrappedPromise((resolve, reject) =\u003e {\n    // ...\n});\n\nexternalSource.on(\"someEvent\", () =\u003e {\n    promise.reject();\n\n    console.log(promise.status); // \"rejected\"\n});\n```\n\n### Flat promise behavior\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst flatPromise = new UnwrappedPromise();\n\nflatPromise.then(() =\u003e console.log(\"Resolved!\"));\n\nflatPromise.resolve();\n\nawait flatPromise; // \"Resolved!\"\n```\n\n### Waiting for settlement\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst promise = new UnwrappedPromise((resolve) =\u003e setTimeout(resolve, 1000));\n\nawait promise.settled; // Resolves in 1 second\n```\n\n### Creating an `UnwrappedPromise` from a regular `Promise`\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst fetchResultPromise = UnwrappedPromise.from(fetch(\"/some/endpoint\"));\n\nconsole.log(fetchResultPromise.status); // \"pending\"\n\nconst result = await fetchResultPromise;\n\nconsole.log(fetchResultPromise.status); // \"resolved\"\n```\n\nKeep in mind the static `UnwrappedPromise.from` method creates a _copy_ of the promise provided as the argument. Forecefully rejecting or resolving the unwrapped promise has no effect on the status of the original promise.\n\n### \"Re-wrapping\" a promise\n\nYou can get the normal promise representation of an unwrapped promise by calling `.rewrap()`.\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst fetchResultUnwrappedPromise = UnwrappedPromise.from(\n    fetch(\"/some/endpoint\")\n);\n\nconsole.log(fetchResultUnwrappedPromise.status); // \"pending\"\n\nconst fetchResultPromise = fetchResultUnwrappedPromise.rewrap();\n\nconsole.log(fetchResultPromise.status); // undefined\n\nconst result = await fetchResultPromise; // works as normal :)\n```\n\n### Resolving from a callback\n\n```js\nimport { readFile } from \"node:fs\";\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst data = await UnwrappedPromise.fromCallback((handle) =\u003e {\n    readFile(\"/path/to/file.txt\", \"utf8\", handle);\n});\n```\n\n### Timer utilities\n\nCreating a promise that will automatically time out after 5 seconds:\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst promise = new Promise((resolve) =\u003e setTimeout(resolve, 10000));\n\nconst unwrappedPromise = UnwrappedPromise.withTimeout(5000, promise);\n\n// Logs \"Timed out!\" after 5 seconds\nawait unwrappedPromise.catch((err) =\u003e console.log(\"Timed out!\"));\n```\n\nAlternative approach with `UnwrappedPromise.timer`:\n\n```js\nimport { UnwrappedPromise } from \"unwrapped-promise\";\n\nconst unwrappedPromise = UnwrappedPromise.timer(10000);\nsetTimeout(unwrappedPromise.reject, 5000);\n\n// Logs \"Timed out!\" after 5 seconds\nawait unwrappedPromise.catch((err) =\u003e console.log(\"Timed out!\"));\n```\n\n## Examples\n\nYou can check out some more complex usage examples [here](https://github.com/jdeurt/unwrapped-promise/tree/main/examples).\n\n## License\n\nMIT © [Juan de Urtubey](https://jdeurt.xyz)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeurt%2Funwrapped-promise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdeurt%2Funwrapped-promise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdeurt%2Funwrapped-promise/lists"}