await-here
A helper wrapper for quick error handling with async/await. TL;DR
Installation
using yarn: yarn add await-here
using npm: npm install await-here --save
Normal ways to handle error earlier
Its 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.
{ const data = await ; // won't be executed any further if above line fails console; ;}
using catch block:
{ try const data = await ; console; ; catch e console; }
Now using await-here
await-here inspired by golang allow you to handle data and error together without hustle:
{ const err data = await ; iferr return ; return ;}
But lets say you want to do some series of async queries like this:
{ try const user = await ; if !user return ; catch e return ; try const userTasks = await ; if !userTasks return ; else ; catch e return ; }
lets see how await-here simplyfies the above example of feting user and its tasks with all possible error cases and values
; { let err user userTasks; err user = await ; if err return ; if !user return ; err userTasks = await ; if err return ; if !userTasks return ;}
Chain
In 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:
{ const err finalFormatToReturn = await ;} what it does is saves from the headache of taking care of error at each step
Supports Typescript 🤟
here<T, E = any>chain<T, R = T, E = any> T: promise resolve typeR: final expected result