{"id":13448513,"url":"https://github.com/Harshit369/await-here","last_synced_at":"2025-03-22T09:31:24.959Z","repository":{"id":32982596,"uuid":"139320874","full_name":"Harshit369/await-here","owner":"Harshit369","description":"A helper package for ES6 async/await","archived":false,"fork":false,"pushed_at":"2023-01-04T21:51:38.000Z","size":270,"stargazers_count":5,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T00:45:05.174Z","etag":null,"topics":["async","await","error-handling","es6","try-catch","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Harshit369.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-01T10:47:48.000Z","updated_at":"2022-02-11T15:41:30.000Z","dependencies_parsed_at":"2023-01-14T22:55:36.860Z","dependency_job_id":null,"html_url":"https://github.com/Harshit369/await-here","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Harshit369%2Fawait-here","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Harshit369%2Fawait-here/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Harshit369%2Fawait-here/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Harshit369%2Fawait-here/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Harshit369","download_url":"https://codeload.github.com/Harshit369/await-here/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244937751,"owners_count":20535124,"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","error-handling","es6","try-catch","typescript"],"created_at":"2024-07-31T05:01:47.700Z","updated_at":"2025-03-22T09:31:24.317Z","avatar_url":"https://github.com/Harshit369.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"## await-here\n\nA helper wrapper for quick error handling with async/await. [TL;DR](#Now-using-await-here)\n\n#### Installation\n\n```\nusing yarn: yarn add await-here\nusing npm: npm install await-here --save\n```\n\n#### Normal ways to handle error earlier\n\nIts somewhat cumbersome for a developer to handle error using async/await. the way below code is written the function will fail silently if the `somethingThatRetunsPromise` fails.\n\n```js\nasync function(cb) {\n  const data = await somethingThatRetunsPromise();\n  // won't be executed any further if above line fails\n  console.log(data);\n  cb(data);\n}\n```\n\nusing catch block:\n\n```js\nasync function asyncOperation(cb) {\n  try {\n    const data = await somethingThatRetunsPromise();\n    console.log(data);\n    cb(data);\n  } catch (e) {\n    console.log(e);\n  }\n}\n```\n\n#### Now using await-here\n\nawait-here inspired by golang allow you to handle data and error together without hustle:\n\n```js\nasync function(cb) {\n  const [err, data] = await somethingThatRetunsPromise();\n  if(err) return alert('something wrong happened');\n  return cb(data);\n}\n```\n\n---\n\nBut lets say you want to do some series of async queries like this:\n\n```js\nasync function asyncOperation(cb) {\n  try {\n    const user = await fetchUser();\n    if (!user) return cb('user not found');\n  } catch (e) {\n    return cb('unable to fetch user details');\n  }\n\n  try {\n    const userTasks = await fetchUserTasks(user.id);\n    if (!userTasks) {\n      return cb('user has no tasks as of now');\n    } else {\n      cb(userTasks);\n    }\n  } catch (e) {\n    return cb('unable to fetch user tasks');\n  }\n}\n```\n\nlets see how **await-here** simplyfies the above example of feting user and its tasks with all possible error cases and values\n\n```js\nimport here from 'await-here';\n\nasync function asyncOperation(cb) {\n  let err, user, userTasks;\n  [err, user] = await here(fetchUser());\n  if (err) return cb('unable to fetch user details');\n  if (!user) return cb('user not found');\n\n  [err, userTasks] = await here(fetchUserTasks(user.id));\n  if (err) return cb('unable to fetch user tasks');\n  if (!userTasks) return cb('user has no tasks as of now');\n}\n```\n\n### Chain\n\nIn adition to error handler there's an additional `chain` function. which as the name suggests lets you chain transformers or promises in order like this:\n\n```js\nasync function seriesOfAsyncOperations() {\n  const [err, finalFormatToReturn] = await chain(\n    fetchUsers(),\n    users =\u003e {\n      return filter(users, user =\u003e user.weLove);\n    },\n    user =\u003e api.fetchUserTasks(user.id),\n    tasks =\u003e filter(tasks, task =\u003e task.unfinished)\n  );\n}\n\nwhat it does is saves from the headache of taking care of error at each step\n```\n\n**Supports Typescript 🤟**\n\n```ts\nhere\u003cT, E = any\u003e\nchain\u003cT, R = T, E = any\u003e\n\nT: promise resolve type\nR: final expected result type after transformations defaults to T\nE: custom error type defaults to any\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHarshit369%2Fawait-here","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHarshit369%2Fawait-here","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHarshit369%2Fawait-here/lists"}