{"id":13448514,"url":"https://github.com/xobotyi/await-of","last_synced_at":"2025-04-04T15:08:04.022Z","repository":{"id":32810341,"uuid":"143302175","full_name":"xobotyi/await-of","owner":"xobotyi","description":"await wrapper for easier errors handling without try-catch","archived":false,"fork":false,"pushed_at":"2023-03-01T01:01:43.000Z","size":1885,"stargazers_count":272,"open_issues_count":9,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T14:07:04.480Z","etag":null,"topics":["async","async-await","await","browser","error-handling","node","promise","try-catch","wrapper"],"latest_commit_sha":null,"homepage":null,"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/xobotyi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"patreon":"xobotyi"}},"created_at":"2018-08-02T13:59:06.000Z","updated_at":"2025-02-28T22:27:27.000Z","dependencies_parsed_at":"2024-01-07T21:07:16.155Z","dependency_job_id":"19cb485b-7425-4feb-a74f-da5ec3ee89d4","html_url":"https://github.com/xobotyi/await-of","commit_stats":{"total_commits":418,"total_committers":6,"mean_commits":69.66666666666667,"dds":0.3397129186602871,"last_synced_commit":"74dfc19419b186fea75bd2fb4270a1e65f4b8e6f"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xobotyi%2Fawait-of","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xobotyi%2Fawait-of/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xobotyi%2Fawait-of/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xobotyi%2Fawait-of/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xobotyi","download_url":"https://codeload.github.com/xobotyi/await-of/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198459,"owners_count":20900080,"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","async-await","await","browser","error-handling","node","promise","try-catch","wrapper"],"created_at":"2024-07-31T05:01:47.727Z","updated_at":"2025-04-04T15:08:03.994Z","avatar_url":"https://github.com/xobotyi.png","language":"TypeScript","funding_links":["https://patreon.com/xobotyi"],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# await-of\n\nawait wrapper for easier errors handling without try-catch\n\n[![NPM Version](https://flat.badgen.net/npm/v/await-of)](https://www.npmjs.com/package/await-of)\n[![NPM Downloads](https://flat.badgen.net/npm/dm/await-of)](https://www.npmjs.com/package/await-of)\n[![NPM Dependents](https://flat.badgen.net/npm/dependents/await-of)](https://www.npmjs.com/package/await-of)\n[![Build](https://img.shields.io/github/workflow/status/xobotyi/await-of/CI?style=flat-square)](https://github.com/xobotyi/await-of/actions)\n[![Coverage](https://flat.badgen.net/codecov/c/github/xobotyi/await-of)](https://app.codecov.io/gh/xobotyi/await-of)\n[![Types](https://flat.badgen.net/npm/types/await-of)](https://www.npmjs.com/package/await-of)\n[![Tree Shaking](https://flat.badgen.net/bundlephobia/tree-shaking/await-of)](https://bundlephobia.com/result?p=await-of)\n\n\u003c/div\u003e\n\n---\n\n\u003cdiv align=\"center\"\u003e❤️Please consider starring this project to show your love and support.🙌\u003c/div\u003e\n\n---\n\n\n## About\n\nES7 `async/await` gives to developers ability to write asynchronous code that look like synchronous. But under the hood it is still just a sugar on top of the ES6 `Promise`.  \nYou can write code that looks clean, but only unless you have to catch errors. To catch thrown error or handle the promise's rejection you have to surround it with `try-catch` block or fallback to pure promises and from that moment visual purity of your code is over.  \nBut there is a solution!☀️  \nI really like the way it's done in **Go**. It has no error throwing mechanism, but has a multi-value return and the common way to handle errors in Go is to return error as a last value, like so:\n\n```go\ndata, err := someErrorFunc(someStuff)\nif err != nil {\n    return err\n}\n```\n\n_But JavaScript has no multi-value return!_ - you would say. Sad, but true.  \nBut!  \nIt has a destructuring assignment and `await-of` gives you ability to do this:\n\n```javascript\nimport { of } from \"await-of\";\n\nasync () =\u003e {\n  let [res, err] = await of(axios.get(\"some.uri/to/get\"));\n\n  if (err) {\n    // rethrow if its not an axios response error\n    if (!err.response) {\n      throw err;\n    }\n\n    res = err.response;\n  }\n\n  const { data, status = 0 } = res;\n\n  console.log(data, status);\n};\n```\n\nThere is no modifications needed in function/promise you want to await - just pass it to the `of()` and whole the magic will be done.\n\n## Installation\n\n```bash\nnpm i --save await-of\n```\n\n## Usage\n\n```javascript\nimport { of } from \"await-of\";\n\nasync function someAsyncStuff() {\n  let error, data;\n\n  // if we don't want to handle error\n  [data] = await of(Promise.reject(new Error(\"ERROR!\")));\n  console.log(data); // undefined\n\n  // if promise was rejected - it's rejection value will be treated as error\n  [, error] = await of(Promise.reject(new Error(\"ERROR!\")));\n  console.log(error); // ERROR!\n\n  // or if promise has any uncaught errors it'll catch them too!\n  [, error] = await of(\n    new Promise(() =\u003e {\n      throw new TypeError(\"ERROR!\");\n    })\n  );\n  console.log(error.message); // ERROR!\n}\n```\n\n## Tests\n\n```bash\n# install dependencies if you haven't yet\nnpm install\nnpm run test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxobotyi%2Fawait-of","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxobotyi%2Fawait-of","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxobotyi%2Fawait-of/lists"}