{"id":22903948,"url":"https://github.com/json2d/await-on","last_synced_at":"2025-05-08T17:21:50.022Z","repository":{"id":32570314,"uuid":"137271875","full_name":"json2d/await-on","owner":"json2d","description":"really simple error handling for await/async","archived":false,"fork":false,"pushed_at":"2022-12-08T02:21:40.000Z","size":335,"stargazers_count":11,"open_issues_count":10,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T05:19:24.672Z","etag":null,"topics":[],"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/json2d.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}},"created_at":"2018-06-13T21:13:45.000Z","updated_at":"2023-07-04T03:40:44.000Z","dependencies_parsed_at":"2023-01-14T21:37:23.673Z","dependency_job_id":null,"html_url":"https://github.com/json2d/await-on","commit_stats":null,"previous_names":["bitstrider/await-on"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fawait-on","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fawait-on/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fawait-on/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/json2d%2Fawait-on/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/json2d","download_url":"https://codeload.github.com/json2d/await-on/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253112359,"owners_count":21856123,"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":[],"created_at":"2024-12-14T02:39:34.092Z","updated_at":"2025-05-08T17:21:49.977Z","avatar_url":"https://github.com/json2d.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 😪 await-on\n![npm](https://img.shields.io/npm/v/await-on.svg)\n![node](https://img.shields.io/node/v/await-on.svg)\n![npm](https://img.shields.io/npm/l/await-on.svg)\n![npm](https://img.shields.io/npm/dt/await-on.svg)\n![Travis](https://img.shields.io/travis/bitstrider/await-on.svg)\n![Coveralls github](https://img.shields.io/coveralls/github/bitstrider/await-on.svg)\n\nreally simple error handling with await/async\n\ninspired by [`await-to-js`](https://github.com/scopsy/await-to-js) whose creator [Dima Grossman](http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/) originally blogged about using destructuring array assignment\n\n## Overview\nThis package basically provides 2 main syntaxes to choose from:\n```javascript\nconst on = require('await-on');\n\nconst fetchData = () =\u003e new Promise(/*...*/);\n\nconst [err, data] = await on(fetchData());\nconst [err, data] = await fetchData().handle(); //prototype extension\n```\n\nThe goal is to avoid the built-in approach using the `try`/`catch` block pattern:\n\n```javascript\ntry{\n\tconst data = await fetchData();\n\tres.send(data);\n}catch(err) {\n\tres.send(err);\n}\n```\n\n## Quick Usage\n\nusing `on` with the function `fetchData` which returns a Promise that resolve to some result `data`:\n```javascript\nconst {on} = require('await-on');\nconst fetchData = () =\u003e new Promise(/*...*/);\n\nasync function foo(req,res) {\n\tconst [err, data] = await on(fetchData());\n\tif(err) res.send(err);\n\telse res.send(data);\n}\n```\n\nusing the prototype extension `handle` on Promise types is a bit cleaner, and its potentially more readable because its also using the same chaining pattern already standard for working with Promises 🌟 :\n\n```javascript\nrequire('await-on');\n\nasync function foo(req,res) {\n\tconst [err, data] = await fetchData().handle();\n\t!err ? res.send(data) : res.send(err);\n}\n```\n\n\n## Type fuzziness\nNon-promises will passthrough same as the behavior of the native `await`\n\n```javascript\nconst [err,answer] = await on(42); //not a promise but ok no big deal\nconsole.log(answer) //\u003e 42\n```\n\n\n## Decorator approach\nA decorator `on.handler` is also provided to wrap promise bearing functions with the handling functionalities:\n\n```javascript\nconst {handler} = require('await-on');\nlet fetchData = () =\u003e new Promise(/*...*/);\nfetchDataAndHandle = handler(fetchData);\n\nasync function foo(req,res) {\n\tconst [err, data] = await fetchDataAndHandle();\n\t!err ? res.send(data) : res.send(err);\n}\n```\n\n## Promises/A+ compliant support\nYou never know what kind of promise you'll get from an external dependency. It can be from Bluebird, Babel polyfill or some other library. `on` should work with any promise object can `then`, such as the one provided by the popular [`bluebird`](https://github.com/petkaantonov/bluebird/) package:\n\n```\nconst {on} = require('await-on');\nconst Bluebird = require('bluebird')\n\nconst fetchData = () =\u003e new Bluebird(/*...*/);\nconst [err, data] = await on(fetchData());\n```\n\n\n## License\nMIT License. See [License](https://github.com/bitstrider/await-on/blob/master/LICENSE) in the repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson2d%2Fawait-on","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjson2d%2Fawait-on","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjson2d%2Fawait-on/lists"}