{"id":16485201,"url":"https://github.com/mrspartak/promises","last_synced_at":"2025-06-13T05:35:21.118Z","repository":{"id":245342856,"uuid":"817973877","full_name":"mrspartak/promises","owner":"mrspartak","description":"Promise utilities","archived":false,"fork":false,"pushed_at":"2024-06-24T13:46:53.000Z","size":273,"stargazers_count":67,"open_issues_count":1,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-15T23:06:18.272Z","etag":null,"topics":["deferred","delay","javascript","library","promise","sleep","timeout","typescript","utility"],"latest_commit_sha":null,"homepage":"https://mrspartak.github.io/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/mrspartak.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-20T20:54:14.000Z","updated_at":"2024-11-02T16:29:41.000Z","dependencies_parsed_at":"2024-11-24T08:01:05.176Z","dependency_job_id":null,"html_url":"https://github.com/mrspartak/promises","commit_stats":null,"previous_names":["mrspartak/promises"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrspartak%2Fpromises","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrspartak%2Fpromises/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrspartak%2Fpromises/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrspartak%2Fpromises/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrspartak","download_url":"https://codeload.github.com/mrspartak/promises/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230907481,"owners_count":18298365,"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":["deferred","delay","javascript","library","promise","sleep","timeout","typescript","utility"],"created_at":"2024-10-11T13:24:36.092Z","updated_at":"2024-12-23T03:06:54.723Z","avatar_url":"https://github.com/mrspartak.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cimg width=\"560\" src=\"https://raw.githubusercontent.com/mrspartak/promises/master/assets/logo.svg\" alt=\"@mrspartak/promises logo\"\u003e\n\u003c/p\u003e\n\u003cbr/\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://npmjs.com/package/@mrspartak/promises\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@mrspartak/promises.svg\" alt=\"npm package\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://npmjs.com/package/@mrspartak/promises\"\u003e\u003cimg src=\"https://img.shields.io/bundlephobia/min/%40mrspartak/promises.svg\" alt=\"bundle size\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://npmjs.com/package/@mrspartak/promises\"\u003e\u003cimg src=\"https://img.shields.io/npm/dw/%40mrspartak%2Fpromises.svg\" alt=\"downloads\"\u003e\u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/mrspartak/promises?branch=master'\u003e\u003cimg src='https://coveralls.io/repos/github/mrspartak/promises/badge.svg?branch=master' alt='Coverage Status' /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\u003cbr/\u003e\n\n# 🔧 Typescript promise utilities\n@mrspartak/promises is a toolkit with useful promise utilities that utilize the power of TypeScript\n\n## Contents\n- [Installation](#Installation)\n- [to](#to---Simplified-Promise-Handling-with-Tuples)\n- [delay](#delay---Pause-Execution-for-a-Specified-Time)\n- [timeout](#timeout---Timeout-a-Promise)\n- [deferred](#deferred---Create-a-Deferred-Promise)\n- [retry](#retry---Retry-a-Promise-Returning-Function)\n- [duration](#duration---Measure-the-Time-Taken-for-a-Promise-to-Resolve)\n\n## Installation\n```sh\n# yarn\nyarn add @mrspartak/promises\n# npm\nnpm i @mrspartak/promises\n# pnpm\npnpm add @mrspartak/promises\n# bun\nbun add @mrspartak/promises\n```\n\n## Functions\n\n### `to` - Simplified Promise Handling with Tuples\n\nThis function helps reduce the verbosity of try/catch blocks by allowing you to handle promise results inline. Inspired by Go's error handling, it returns a tuple with either an error or the result, making your asynchronous code cleaner and more maintainable.\n\n**Pros:**\n- **✨ Cleaner Code**: Reduces the verbosity and complexity of using multiple try/catch blocks, leading to more readable and maintainable code.\n- **📝 No Need for Predefined Variables**: Eliminates the need to declare variables before the try/catch block, simplifying variable management and ensuring proper typing.\n- **📏 Forces a Consistent Error Handling Pattern**: Encourages a unified and specific way of dealing with errors, improving code consistency across the project.\n\n```ts \nimport { to } from \"@mrspartak/promises\"\nimport { api } from \"./api\"\n\n// Simple tuple destructuring\nconst [apiError, user] = await to(api.get(\"/me\"))\nif (apiError) {\n  // Handle error\n}\n\n// Using finally\n$component.isLoading = true\nconst [apiError, status] = await to(api.post(\"/me/status\", { status: \"online\" }), () =\u003e {\n  $component.isLoading = false\n})\nif (apiError) {\n  // Handle error\n}\n```\n\n### `delay` - Pause Execution for a Specified Time\n\nThe delay function pauses the execution of your code for a specified number of milliseconds. This can be useful in various scenarios, such as waiting for an operation to complete, introducing a delay between retries, or simply pausing execution for debugging purposes.\n\n! The function has alias `sleep`\n\n```ts\nimport { delay, sleep } from \"@mrspartak/promises\"\nimport { parsePage } from \"./parser\"\n\nfor (let i = 0; i \u003c 10; i++) {\n  // Parse the page and wait for 1 second before parsing the next page\n  const pageData = await parsePage(i)\n  await delay(1000)\n}\n\n// You can also use alias sleep instead of delay\nawait sleep(1000)\n```\n\n### `timeout` - Timeout a Promise\n\nThe timeout function allows you to set a maximum time for a promise to resolve. If the promise does not resolve within the specified time, an error is thrown.\n\n```ts\nimport { timeout } from \"@mrspartak/promises\"\nimport { api } from \"./api\"\n\n// Can be used as a race condition\nconst [error, user] = await timeout(api.getUser(), 1000)\nif (error) {\n  // error can be either a timeout error or an error from the api\n}\n```\n\n### `deferred` - Create a Deferred Promise\n\nThe deferred function allows you to manually resolve or reject a promise at a later time. This can be useful in scenarios where you need to control the timing of the resolution or rejection, such as in testing or when dealing with asynchronous operations that don't natively return promises.\n\n```ts\nimport { deferred } from \"@mrspartak/promises\"\n\n// Create a deferred promise\nconst { promise, resolve, reject } = deferred\u003cvoid\u003e()\n\nsetTimeout(() =\u003e {\n  // Resolve the promise\n  resolve()\n}, 1000)\n\nawait promise // Will wait for 1 second before resolving\n```\n\n### `retry` - Retry a Promise-Returning Function\n\nThe `retry` function allows you to retry a promise-returning function a specified number of times with an optional delay between attempts if it fails. This can be useful for handling transient errors, such as network requests that may occasionally fail.\n\n```ts\nimport { retry } from \"@mrspartak/promises\"\nimport { apiCall } from \"./api\"\n\n// Retry the API call up to 3 times with a delay of 1000 milliseconds between attempts\nconst [error, result] = await retry(() =\u003e apiCall(), 3, { delay: 1000 })\nif (error) {\n  // error will always be an error returneb by a promise rejection\n}\n```\n\n### `duration` - Measure the Time Taken for a Promise to Resolve\n\nThe `duration` function allows you to measure the time it takes for a promise to resolve or reject. This is useful for performance monitoring and debugging asynchronous operations in your code.\n\n```ts\nimport { duration } from \"@mrspartak/promises\"\nimport { apiCall } from \"./api\"\n\n// Measure the time taken to resolve the API call\nconst [error, result, time] = await duration(apiCall())\nif (error) {\n  // Handle error\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrspartak%2Fpromises","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrspartak%2Fpromises","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrspartak%2Fpromises/lists"}