{"id":13486770,"url":"https://github.com/open-draft/until","last_synced_at":"2025-03-27T21:30:58.460Z","repository":{"id":42912633,"uuid":"249745612","full_name":"open-draft/until","owner":"open-draft","description":"Gracefully handle Promises using async/await without try/catch.","archived":false,"fork":false,"pushed_at":"2024-02-11T12:34:18.000Z","size":712,"stargazers_count":285,"open_issues_count":4,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-04-25T22:00:33.138Z","etag":null,"topics":["async","await","promise","typescript","until"],"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/open-draft.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}},"created_at":"2020-03-24T15:30:26.000Z","updated_at":"2024-04-24T06:06:05.000Z","dependencies_parsed_at":"2024-02-11T13:36:16.147Z","dependency_job_id":"b9094cc1-4af5-4b8e-a69d-4e1454d4d61b","html_url":"https://github.com/open-draft/until","commit_stats":{"total_commits":51,"total_committers":7,"mean_commits":7.285714285714286,"dds":"0.37254901960784315","last_synced_commit":"b43a2f1792a821c9e008e9d1fa0e7774b5107f00"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-draft%2Funtil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-draft%2Funtil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-draft%2Funtil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-draft%2Funtil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-draft","download_url":"https://codeload.github.com/open-draft/until/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245927169,"owners_count":20695179,"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":["async","await","promise","typescript","until"],"created_at":"2024-07-31T18:00:50.938Z","updated_at":"2025-03-27T21:30:58.444Z","avatar_url":"https://github.com/open-draft.png","language":"TypeScript","readme":"[![Latest release](https://img.shields.io/npm/v/@open-draft/until.svg)](https://www.npmjs.com/package/@open-draft/until)\n\n# `until`\n\nGracefully handle a Promise using `async`/`await`.\n\n## Why?\n\nWith the addition of `async`/`await` keywords in ECMAScript 2017 the handling of Promises became much easier. However, one must keep in mind that the `await` keyword provides no standard error handling API. Consider this usage:\n\n```js\nasync function getUser(id) {\n  const data = await fetchUser(id)\n  // Work with \"data\"...\n}\n```\n\nIn case `fetchUser()` throws an error, the entire `getUser()` function's scope will terminate. Because of this, it's recommended to implement error handling using `try`/`catch` block wrapping `await` expressions:\n\n```js\nasync function getUser(id){\n  let data = null\n\n  try {\n    data = await asyncAction()\n  } catch (error) {\n    console.error(error)\n  }\n\n  // Work with \"data\"...\n}\n```\n\nWhile this is a semantically valid approach, constructing `try`/`catch` around each awaited operation may be tedious and get overlooked at times. Such error handling also introduces separate closures for execution and error scenarios of an asynchronous operation.\n\nThis library encapsulates the `try`/`catch` error handling in a utility function that does not create a separate closure and exposes a NodeJS-friendly API to work with errors and resolved data.\n\n## Getting started\n\n### Install\n\n```bash\nnpm install @open-draft/until\n```\n\n### Usage\n\n```js\nimport { until } from '@open-draft/until'\n\nasync function getUserById(id) {\n  const { error, data } = await until(() =\u003e fetchUser(id))\n\n  if (error) {\n    return handleError(error)\n  }\n\n  return data\n}\n```\n\n### Usage with TypeScript\n\n```ts\nimport { until } from '@open-draft/until'\n\ninterface User {\n  firstName: string\n  age: number\n}\n\ninterface UserFetchError {\n  type: 'FORBIDDEN' | 'NOT_FOUND'\n  message?: string\n}\n\nasync function getUserById(id: string) {\n  const { error, data } = await until\u003cUserFetchError, User\u003e(() =\u003e fetchUser(id))\n\n  if (error) {\n    return handleError(error.type, error.message)\n  }\n\n  return data.firstName\n}\n```\n\n## Frequently asked questions\n\n### Why does `until` accept a function and not a `Promise` directly?\n\nThis has been intentionally introduced to await a single logical unit as opposed to a single `Promise`.\n\n```js\n// Notice how a single \"until\" invocation can handle\n// a rather complex piece of logic. This way any rejections\n// or exceptions happening within the given function\n// can be handled via the same \"error\".\nconst { error, data } = until(async () =\u003e {\n  const user = await fetchUser()\n  const nextUser = normalizeUser(user)\n  const transaction = await saveModel('user', user)\n\n  invariant(transaction.status === 'OK', 'Saving user failed')\n\n  return transaction.result\n})\n\nif (error) {\n  // Handle any exceptions happened within the function.\n}\n```\n\n### Why does `until` return an object and not an array?\n\nThe `until` function used to return an array of shape `[error, data]` prior to `2.0.0`. That has been changed, however, to get proper type-safety using discriminated union type.\n\nCompare these two examples:\n\n```ts\nconst [error, data] = await until(() =\u003e action())\n\nif (error) {\n  return null\n}\n\n// Data still has ambiguous \"DataType | null\" type here\n// even after you've checked and handled the \"error\" above.\nconsole.log(data)\n```\n\n```ts\nconst result = await until(() =\u003e action())\n\n// At this point, \"data\" is ambiguous \"DataType | null\"\n// which is correct, as you haven't checked nor handled the \"error\".\n\nif (result.error) {\n  return null\n}\n\n// Data is strict \"DataType\" since you've handled the \"error\" above.\nconsole.log(result.data)\n```\n\n\u003e It's crucial to keep the entire result of the `Promise` in a single variable and not destructure it. TypeScript will always keep the type of `error` and `data` as it was upon destructuring, ignoring any type guards you may perform later on.\n\n## Special thanks\n\n- [giuseppegurgone](https://twitter.com/giuseppegurgone) for the discussion about the original `until` API.\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-draft%2Funtil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-draft%2Funtil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-draft%2Funtil/lists"}