{"id":20093890,"url":"https://github.com/api3dao/promise-utils","last_synced_at":"2025-06-14T14:06:03.166Z","repository":{"id":37960367,"uuid":"460079093","full_name":"api3dao/promise-utils","owner":"api3dao","description":"A simple package for a functional and typesafe error handling","archived":false,"fork":false,"pushed_at":"2025-04-07T15:55:24.000Z","size":660,"stargazers_count":7,"open_issues_count":5,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-06-07T03:04:40.140Z","etag":null,"topics":["promise","typescript","utilities"],"latest_commit_sha":null,"homepage":"https://github.com/api3dao/promise-utils","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/api3dao.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,"zenodo":null}},"created_at":"2022-02-16T16:11:27.000Z","updated_at":"2025-04-07T15:55:28.000Z","dependencies_parsed_at":"2024-02-19T20:01:15.095Z","dependency_job_id":"b30b7e12-1794-44ec-9cae-810f3a8082c6","html_url":"https://github.com/api3dao/promise-utils","commit_stats":{"total_commits":163,"total_committers":6,"mean_commits":"27.166666666666668","dds":0.558282208588957,"last_synced_commit":"b4e499f6748503ea13e2865961c91ab5b0aeab8c"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/api3dao/promise-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api3dao%2Fpromise-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api3dao%2Fpromise-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api3dao%2Fpromise-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api3dao%2Fpromise-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/api3dao","download_url":"https://codeload.github.com/api3dao/promise-utils/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/api3dao%2Fpromise-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259827647,"owners_count":22917711,"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":["promise","typescript","utilities"],"created_at":"2024-11-13T16:48:40.106Z","updated_at":"2025-06-14T14:06:03.139Z","avatar_url":"https://github.com/api3dao.png","language":"TypeScript","readme":"# promise-utils [![ContinuousBuild](https://github.com/api3dao/promise-utils/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/api3dao/promise-utils/actions/workflows/main.yml)\n\n\u003e A simple package for a functional and typesafe error handling with zero dependencies\n\n## Installation\n\nTo install this package run either:\n\n`yarn add @api3/promise-utils`\n\nor if you use npm:\n\n`npm install @api3/promise-utils --save`\n\n## Usage\n\nThe API is small and well focused on providing [more concise error handling](#motivation). The main functions of this\npackage are `go` and `goSync` functions. They accept a function to execute, and additionally `go` accepts an optional\n`GoAsyncOptions` object as the second parameter. If the function executes without an error, a success response with the\ndata is returned, otherwise an error response is returned.\n\n\u003c!-- NOTE: Keep in sync with the \"documentation snippets are valid\" test --\u003e\n\n```ts\n// Imagine an async function for fetching API data\nconst goFetchData = await go(() =\u003e fetchData('users'));\n// The \"goFetchData\" value is either: {success: true, data: ...} or {success: false, error: ...}\nif (goFetchData.success) {\n  const data = goFetchData.data\n  ...\n}\n```\n\nor:\n\n```ts\n// Imagine an async function for fetching API data\n// If the fetch data is a non class function returning a promise, you can drop the arrow function\nconst goFetchData = await go(() =\u003e fetchData('users'));\n// The \"goFetchData\" value is either: {success: true, data: ...} or {success: false, error: ...}\nif (!goFetchData.success) {\n  const error = goFetchData.error\n  ...\n}\n```\n\nand with `GoAsyncOptions`:\n\n```ts\n// The `fetchData` function will be retried a maximum of 2 times on error, with each attempt having\n// a timeout of 5 seconds and a total timeout 10 seconds (shared among all attempts and delays).\nconst goFetchData = await go(() =\u003e fetchData('users'), { retries: 2, attemptTimeoutMs: 5_000, totalTimeoutMs: 10_000 });\n...\n```\n\nand for synchronous functions:\n\n```ts\nconst someData = ...\n// Imagine a synchronous function for parsing data\nconst goParseData = goSync(() =\u003e parseData(someData));\n// The goParseData value is either: {success: true, data: ...} or {success: false, error: ...}\nif (goParseData.success) {\n  const data = goParseData.data\n  ...\n}\n```\n\nThe return value from the promise utils functions works very well with TypeScript inference. When you check the the\n`success` property, TypeScript will infer the correct response type.\n\n## API\n\nThe full `promise-utils` API consists of the following functions:\n\n- `go(asyncFn, options)` - Executes the `asyncFn` and returns a response of type `GoResult`\n- `goSync(fn)` - Executes the `fn` and returns a response of type `GoResult`\n- `assertGoSuccess(goRes)` - Verifies that the `goRes` is a success response (`GoResultSuccess` type) and throws\n  otherwise.\n- `assertGoError(goRes)` - Verifies that the `goRes` is an error response (`GoResultError` type) and throws otherwise.\n- `success(value)` - Creates a successful result value, specifically `{success: true, data: value}`\n- `fail(error)` - Creates an error result, specifically `{success: false, error: error}`\n\nand the following Typescript types:\n\n- ```ts\n  type GoResult\u003cT\u003e = { data: T; success: true };\n  ```\n- ```ts\n  type GoResultSuccess\u003cE extends Error = Error\u003e = { error: E; success: false };\n  ```\n- ```ts\n  type GoResultError\u003cT, E extends Error = Error\u003e = GoResultSuccess\u003cT\u003e | GoResultError\u003cE\u003e;\n  ```\n- ```ts\n  interface GoAsyncOptions\u003cE extends Error = Error\u003e {\n    retries?: number; // Number of retries to attempt if the go callback is unsuccessful.\n    attemptTimeoutMs?: number | number[]; // The timeout for each attempt. Can provide an array for different timeouts for each attempt. If the array is shorter than the number of retries, the last value is used for all remaining attempts, if the length of the array is longer than the number of retries, the extra values are ignored.\n    totalTimeoutMs?: number; // The maximum timeout for all attempts and delays. No more retries are performed after this timeout.\n    delay?: StaticDelayOptions | RandomDelayOptions; // Type of the delay before each attempt. There is no delay before the first request.\n    onAttemptError?: (goRes: GoResultError\u003cE\u003e) =\u003e void; // Callback invoked after each failed attempt is completed. This callback does not fire for the last attempt or when a \"totalTimeoutMs\" is exceeded (these should be handled explicitly with the result of \"go\" call).\n  }\n  ```\n- ```ts\n  interface StaticDelayOptions {\n    type: 'static';\n    delayMs: number;\n  }\n  ```\n- ```ts\n  interface RandomDelayOptions {\n    type: 'random';\n    minDelayMs: number;\n    maxDelayMs: number;\n  }\n  ```\n\nCareful, the `attemptTimeoutMs` value of `0` means timeout of 0 ms. If you want to have infinite timeout omit the key or\nset it to `undefined`.\n\nThe last exported value is a `GoWrappedError` class which wraps an error which happens in go callback. The difference\nbetween `GoWrappedError` and regular `Error` class is that you can access `GoWrappedError.reason` to get the original\nvalue which was thrown by the function.\n\nTake a look at the [implementation](https://github.com/api3dao/promise-utils/blob/main/src/index.ts) and\n[tests](https://github.com/api3dao/promise-utils/blob/main/src/index.test.ts) for detailed examples and usage.\n\n## Motivation\n\n### Verbosity and interoperability of try-catch pattern\n\n```ts\n// Verbose try catch\ntry {\n  const data = await someAsyncCall();\n  ...\n} catch (e) {\n  // The \"e\" is \"unknown\" because any value can be thrown in Javascript so casting is needed\n  return logError((e as MyError).reason);\n}\n\n// Compare it to simpler version using go\nconst goRes = await go\u003cMyData, MyError\u003e(someAsyncCall);\nif (!goRes.success) return logError(goRes.error.reason);\n// At this point TypeScript infers that the error was handled and \"goRes\" must be a success response\nconst data = goRes.data;\n...\n```\n\nAlso, think about what happens when you want to handle multiple \"can fail\" operations in a single function call. You can\neither:\n\n1. Have them in a same try catch block - but then it's difficult to differentiate between what error has been thrown.\n   Also this usually leads to a lot of code inside a try block and the catch clause acts more like \"catch anything\".\n2. Use nested try catch blocks - but this hurts readability and forces you into the\n   [callback hell pattern](http://callbackhell.com/).\n\n### Consistent throwing of an `Error` instance\n\nJavaScript supports throwing any expression, not just `Error` instances. This is also a reason why TypeScript infers the\nerror as `unknown` or `any` (see:\n[useUnknownInCatchVariables](https://www.typescriptlang.org/tsconfig#useUnknownInCatchVariables)).\n\nThe error response from `go` and `goSync` always return an instance of the `Error` class. Of course, throwing custom\nerrors (derived from `Error`) is supported.\n\n### Intentionally limited feature set\n\nThe go utils by design offer only very basic timeout and retry capabilities as these are often application specific and\ncould quickly result in bloated configuration. If you are looking for more complex features, consider using one of the\nalternatives, e.g. https://github.com/lifeomic/attempt\n\n## Limitations\n\nThere is a limitation when using class functions due to how javascript\n[this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) works.\n\n```ts\nclass MyClass {\n  constructor() {}\n  get() {\n    return this._get();\n  }\n  _get() {\n    return '123';\n  }\n}\n\nconst myClass = new MyClass();\nconst resWorks = goSync(() =\u003e myClass.get()); // This works\n// However, seeing the line above it may be tempting to rewrite it to\nconst resFails = goSync(myClass.get); // This doesn't work\n```\n\nThe problem is that the `this` keyword is determined by how a function is called and in the second example, the `this`\ninside the `get` function is `undefined` which makes the `this._get()` throw an error.\n\n## Developer documentation\n\n### Release\n\nTo release a new version follow these steps:\n\n1. `yarn \u0026\u0026 yarn build`\n2. `yarn version` and choose the version to be released\n3. `yarn publish --access public`\n4. `git push --follow-tags`\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapi3dao%2Fpromise-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapi3dao%2Fpromise-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapi3dao%2Fpromise-utils/lists"}