{"id":15674731,"url":"https://github.com/kyleross/await-handler","last_synced_at":"2025-07-16T01:10:18.333Z","repository":{"id":75753479,"uuid":"109291875","full_name":"KyleRoss/await-handler","owner":"KyleRoss","description":"Basic wrapper for await that allows handling of errors without try/catch blocks","archived":false,"fork":false,"pushed_at":"2022-11-18T17:29:59.000Z","size":15,"stargazers_count":15,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-04T09:30:37.725Z","etag":null,"topics":["async-await","await","error-handling","handler","node","node8","nodejs","npm-module","promise"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/KyleRoss.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}},"created_at":"2017-11-02T16:35:17.000Z","updated_at":"2024-09-20T09:01:58.000Z","dependencies_parsed_at":"2023-04-11T20:45:41.483Z","dependency_job_id":null,"html_url":"https://github.com/KyleRoss/await-handler","commit_stats":{"total_commits":23,"total_committers":1,"mean_commits":23.0,"dds":0.0,"last_synced_commit":"4b704753f91f23eced1c996e16c2732f36a6ebe1"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2Fawait-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2Fawait-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2Fawait-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KyleRoss%2Fawait-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KyleRoss","download_url":"https://codeload.github.com/KyleRoss/await-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252770993,"owners_count":21801631,"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","await","error-handling","handler","node","node8","nodejs","npm-module","promise"],"created_at":"2024-10-03T15:49:38.678Z","updated_at":"2025-05-06T21:26:23.676Z","avatar_url":"https://github.com/KyleRoss.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# await-handler\n[![npm](https://img.shields.io/npm/v/await-handler.svg?style=for-the-badge)](https://www.npmjs.com/package/await-handler) [![npm](https://img.shields.io/npm/dt/await-handler.svg?style=for-the-badge)](https://www.npmjs.com/package/await-handler) [![David](https://img.shields.io/david/KyleRoss/await-handler.svg?style=for-the-badge)](https://david-dm.org/KyleRoss/await-handler) [![Travis](https://img.shields.io/travis/KyleRoss/await-handler/master.svg?style=for-the-badge)](https://travis-ci.org/KyleRoss/await-handler) [![license](https://img.shields.io/github/license/KyleRoss/await-handler.svg?style=for-the-badge)](https://github.com/KyleRoss/await-handler/blob/master/LICENSE) [![Beerpay](https://img.shields.io/beerpay/KyleRoss/await-handler.svg?style=for-the-badge)](https://beerpay.io/KyleRoss/await-handler)\n\nSimple signature to ease the paid of catching errors using async/await. This module will allow a simple method of catching errors from an `await` handler without the need to wrap everything in try/catch blocks. This module is a Node.js only version based on [await-to-js](https://github.com/scopsy/await-to-js) minus the typescript aspect. Credit for this module goes to [Dima Grossman](http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/), as it was based off the code provided.\n\nYou continue to use async/await normally, except to wrap the function you are \"awaiting\", in this module to allow destructuring the returned array into variables. This is similar to the golang error handling syntax.\n\n**NOTE:** This module works in Node 6+, but in order to use async/await, you need to use Node 8+ or compile with Babel.\n\n## Install\nInstall via NPM:\n```\nnpm i await-handler --save\n```\n\n## Usage\n```js\nconst on = require('await-handler');\n\nasync function asyncFunctionExample() {\n    let [err, result] = await on(myAsyncTask());\n    if(err) {\n        throw err;\n    }\n    \n    // ... handle the result\n    console.log(result);\n}\n```\n\n## API\n\n### on(promise[, errorProps])\n**Type:** Function\n\nAdds handler to `promise` in order to return an array which can be destructured. Optionally add additional properties to the returned error by providing an Object to `errorProps`.\n\n| Argument   | Required? | Type    | Description                                                |\n|------------|-----------|---------|------------------------------------------------------------|\n| promise    | Yes       | Promise | Promise to wrap and return results for.                    |\n| errorProps | No        | Object  | Optional object to append to the `Error` if one is thrown. |\n\n**Examples:**\n```js\nasync function basicExample() {\n    let [err, result] = await on(myAsyncTask());\n    if(err) throw err;\n    \n    // ... handle the result\n    console.log(result);\n}\n\nasync function errorPropsExample() {\n    let [err, result] = await on(myAsyncTask(), { customMessage: 'Something failed!' });\n    if(err) {\n        console.error(err.customMessage);\n        return process.exit(1);\n    }\n    \n    // ... handle the result\n    console.log(result);\n}\n```\n\n###### Returns _{Promise\u0026lt;Array\u0026gt;}_\n\u003e Returns Promise that resolves with array signature `[error, results]`. If an error is thrown, `error` will be the the rejection from the promise and `results` will be `undefined`. If an error is not thrown, `error` will be `null` and `results` will be the resolved value from the promise.\n\n---\n\n## Tests\nTo run the tests:\n\n```\nnpm install\nnpm run test\n```\n\n## License\nMIT License. See [License](https://github.com/KyleRoss/await-handler/blob/master/LICENSE) in the repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyleross%2Fawait-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyleross%2Fawait-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyleross%2Fawait-handler/lists"}