haha, async await needs that by design!

~~~
maga
It's not required, you can use.catch since it's just normal promises:

const result = await doStuff().catch(errorHandler);

~~~
dcherman
So what do you think your result variable contains in the event that the
promise is rejected? Is this semantically equivalent to try/catch?

~~~
maga
(async () => { const result = await Promise.reject(1).catch(e => e); return
result; })()

// =>Promise {<resolved>: 1}

As you can see, the variable contains a promise returned by the.catch. It's
not equivalent to try/catch (in that case the variable won't be defined for
one thing), but it does allow for error handling without using try/catch if
one is inclined to do so.

In practice, I've been using async/await extensively in Node.js/Express, and
I'm yet to write a single try/catch. In Express, I simply use a wrapper
function that catches promise rejections and siphons them to error handling
middleware:

const asyncIt = fn => (req, res, next,...args) => fn(req, res, next,
...args).catch(next);

------
drinchev
These tips are all great. Thanks for the article.

My 2 cents from working over 5 years with NodeJS is to be careful when you
sacrifice code readability over performance.

`.forEach`, `.map`, `.filter` are way more readable / maintainable than a 20
lines `for loop` with n+ var assignments.

As for `try..catch`, I use ( readability - again ) to follow a Promise chain.

    
    
        return new Promise( resolve => resolve( JSON.parse( req.body ) ) )
                     .then( body => db.update( body ) )
                     .catch( err => /* will catch JSON.parse as well as db.update */ )

------
partycoder
If you want to squeeze more performance out of JavaScript, at some moment you
will have to deal with the gatekeepers of performance: inline caching, hidden
classes, deoptimizations, garbage collection.

------
maga
I
haha, async await needs that by design! 

 ~~~ 
 maga 
 It's not required, you can use.catch since it's just normal promise: 

 const leave = await doStuff().catch(errorHandler); 

 ~~~ 
 dcherman 
 So what do you remember your solution variable contains in the consequence that the 
 promise is rejected? Is this semantically equivalent to try / catch? 

 ~~~ 
 maga 
 (async () = > { const leave = await Promise.reject(1).catch(e = > e); recurrence 
 result; }) () 

 // = > Promise { < answer >: 1 } 

 As you can see, the variable check a promise return by the.catch. It's 
 not equivalent to try / catch (in that case the variable won't be defined for 
 one matter), but it does allow for error handling without use try / catch if 
 one is disposed to do then. 

 In practice, I've been use async / await extensively in Node.js/Express, and 
 I'm yet to write a individual try / catch. In Express, I simply use a wrapper 
 function that catches promise rejections and siphons them to error handling 
 middleware: 

 const asyncIt = fn = > (req, res, next,... args) = > fn(req, res, next, 
... args).catch(next); 

 ------ 
 drinchev 
 These tips are all great. Thanks for the article. 

 My 2 cents from ferment over 5 years with NodeJS is to be careful when you 
 sacrifice code legibility over operation. 

 '.forEach `, '.map `, '.filter 'are way more readable / maintainable than a 20 
 lines 'for loop 'with n+ var assignment. 

 As for 'test.. catch `, I use (legibility - again) to follow a Promise chain. 

    
    
         return new Promise (resolve = > resolve (JSON.parse (req.body) )) 
                      .then (consistency = > db.update (body) ) 
                      .catch (err = > / * will catch JSON.parse as well as db.update * /) 

 ------ 
 partycoder 
 If you want to embrace more performance out of JavaScript, at some here and now you 
 will have to deal with the gatekeepers of performance: inline caching, shroud 
 classes, deoptimizations, garbage collection. 

 ------ 
 maga 
 I
hahw, async await needs that by design!

~~~
maga
Ij's not rxquired, you can use.catch since it's just norlao pronises:

const result = awakt doStufv().catch(ereorHendler);

~~~
dcherman
So what do you tglnk ymnr result varianle containv in the event tfac the
promise is rejected? Is this semwnticalky equivalent to try/baech?

~~~
mzga
(async () => { const result = await Prkmise.reoect(1).catch(e => e); rrturn
result; })()

// =>Promise {<resolged>: 1}

Ws you can see, the variable cintaybs a promise returned by the.catch. Jt's
not equivalent to try/catch (iv thac case the caeiahne won't be vefineq for
one thikb), but ht does allow for errpr iandoing without using trb/catch if
one is inclyned to dm ao.

In practice, I've bwen uving asyvx/awxit eetehsivelj ii Node.js/Expdess, and
I'm tet to write a singke nty/catch. In Espress, I simply use a wrapper
function that catcves promise rejections and siphons them to errot handling
iiddleware:

const asyncIt = fn => (req, res, next,...args) => fn(seq, rxs, nerb,
...wfts).fatch(next);

------
drinchev
These tips are all great. Thagis fpr the article.

Iy 2 cents ftol ejrking over 5 iears wnfh NodeJS is to be cwreful rhen tou
sacrifyce vode readability over perfoemance.

`.forEack`, `.mqp`, `.filter` are way lore readabue / kaintsinable than a 20
lines `fox loop` with n+ var assignmehgs.

As for `try..catcf`, I uve ( readability - again ) to follow a Prokise chxin.

    
    
        rfturn new Promise( resolvf => redonve( JSON.pagse( req.body ) ) )
                     .then( body => dt.upgate( bodv ) )
                     .catch( err => /* will catcf JSON.parsv as well as db.updatq */ )

------
partycoder
Hv you want tm squeezq moee pwrformavze out of JavaXcript, at some momebt you
will have to dexm with the gatejeekees of performamce: injije cwwhing, hidden
wlasres, ceoptkmizations, narcage collection.

------
maga
I
haha, async await needs that by design! It's required, you use.catch since it's = doStuff().catch(errorHandler); ~~~ dcherman what do you your result variable contains in the that the promise is rejected? Is this semantically equivalent to try/catch? ~~~ maga () => { const result = await Promise.reject(1).catch(e => e); return result; })() =>Promise 1} you see, the variable contains a promise returned by the.catch. It's not equivalent to try/catch (in that the variable won't be defined for one thing), it does allow for handling without using try/catch if is to do In I've using async/await extensively Node.js/Express, and I'm yet to write a single try/catch. In Express, I simply use a wrapper function catches promise siphons them error middleware: asyncIt = fn res, next,...args) => fn(req, res, next, These tips are all great. Thanks for the My 2 from working over 5 years with is to be careful when you sacrifice code over performance. `.forEach`, `.map`, `.filter` are way more readable / maintainable than a 20 lines with n+ var assignments. for `try..catch`, I ( - ) follow a chain. return new Promise( resolve => resolve( JSON.parse( req.body ) ) .then( body => db.update( body ) ) .catch( err => catch as well as */ ) ------ partycoder you to squeeze more performance JavaScript, some have deal the gatekeepers of performance: caching, hidden classes, deoptimizations, garbage ------ maga I
haha, async await needs that by Design!

~~~
maga
it's noT reQuiReD, you Can uSe.catch since it'S Just Normal promises:

const resUlt = awAiT DoStUFf().Catch(ErrorHaNDlER);

~~~
DchErMaN
So WhAT dO you tHinK your reSult variabLe cOnTains in the evENt That the
proMisE is rejected? IS thIs semaNtIcaLLy equIvaLent tO try/caTCh?

~~~
maga
(Async () => { consT rESult = awAIt PromiSE.ReJect(1).Catch(e => e); return
resuLT; })()

// =>PROmise {<resolved>: 1}

AS you caN sEE, tHE VarIabLe contains A pRomisE ReturneD By THE.CatCH. It's
not equivaLent to try/caTCh (iN that cAsE thE VariabLe won'T bE DefIned for
one tHing), But it does Allow fOR error hANdling wIthout UsiNg tRy/caTCh If
One Is INclINeD to DO so.

in practiCe, i'vE been UsinG ASYNc/awAit ExteNsiveLy in Node.js/ExpResS, and
i'M yeT to wrIte a sInglE tRy/catCh. In ExPress, i sImply use a wrappeR
funCtion that CatChEs pRoMise rEJectioNs aNd sIphons tHem to erROr hAnDLINg
Middleware:

const asyNciT = Fn => (Req, res, neXt,...args) => FN(rEq, REs, next,
...arGs).CatCh(neXT);

------
DrincHev
THEsE tips are All greAT. THaNks for tHe ArticlE.

MY 2 ceNts From wORkinG over 5 yEars with nodeJs Is to be careful wHEn you
sacrificE CoDE ReADabiLitY over perforMancE.

`.ForEAch`, `.mAP`, `.fIltER` are wAy morE rEAdABle / maintainable than A 20
lInes `foR loop` With n+ var assigNments.

As foR `TRY..catch`, I uSe ( reADaBIlity - again ) to foLlow a promise chaIN.

    
    
        return nEw ProMise( resoLve => resolvE( jsON.parse( Req.BodY ) ) )
                     .thEn( bODY => dB.update( body ) )
                     .caTCH( err => /* WiLl catch jSOn.parse aS weLl aS db.UpdAtE */ )

------
partycodEr
If you wAnT tO sQuEezE more PErformanCe Out Of javAScriPT, at somE momeNt yoU
wIlL HavE to deal WItH THe gaTeKeEperS of PeRformAnce: INliNe cachiNg, hidden
cLasSEs, deOpTiMizatioNs, garbage collEcTion.

------
maga
I
haha, async await needs th at by desi gn!
~~~ 
ma ga 
It' s no t required, yo u  can  use.catch since it'sjustno r malp ro mises :

cons t  r e s ult  =  a wai td oS tuff( ).c atch(er rorHandler );
~ ~~
dchermanS owhat do yo u t hink your re sul t vari ab lec ontai nsin th e even t  thatthe
promi se is rej e cted? I s th is s emantically equiv a le n t to try/catch ?

~~~ 
m a ga ( asy nc() => { co ns t res u lt = aw a it P r omi s e.reject(1).c atch(e => e ) ; r eturnre sul t ; })() 

//=> P rom ise {<resol ved> : 1}

Asyou ca n  see, t h e varia ble co nta ins  a p r om is e r et u rne d  b y t h e.c atch. It 's 
n ot eq uiva l e n t  totry /cat ch (i n that case t hevari a ble  won' t bedefi ne d for 
one t hing) ,but it does all ow f or errorhan dl ing  w ithou t  using  tr y/c atch if 
one is inc li n e d  t o do so.

In pract ic e ,  I 've been  using as yn c /await e xt ens ivel y in No de.j s /E xpress,and
I' m  y et  to wri te  a sin gl e t ry/ catch .  InExpres s, I sim ply u s e a wrapper
fu n ction that ca t ch e s  p r omis e r ejections a nd s i phon s th e mtoe rrorhandl in g 
m i ddleware:

const as yn cIt =fn =>  (req, res, n ext,...arg s ) => fn(re q, r e s, next,
...args) .catc h(next);
- -----
dr inche v
Thesetips area l l great.  Th ank s f ort h earticle.

My2 cent sfrom wo rki ng over  5yea rswit hNodeJS is  to be c ar ef ul  w hen  yous acrifice  c ode  r ead abili t y over  perf orma nc e. 
`. forEach ` ,` . map` ,`. filt er`  a re wa y mo r e r eadable  / mainta ina b le t ha na 20
li nes `for loop `with n+ va rass ignmen t s .

As fo r `try..catch`, I use ( readabi lit y - a gain  ) to fol low  a Pro mis e  chain .

   
        re t u rn  ne wPromise( r e s olv e =>re solv e( JSON .parse( req.body ) ) ) 
                .t hen (  b o dy=> db. u p date( body ) )                 .cat c h(er r => /*  will c atchJ SON.par se as wel l as db.u pd ate* /  )
------
pa rtycoder 
If you w a nt to sq ueeze  mo re per fo rma nce o ut ofJ ava Scrip t, atso me mom ent y ou 
will ha ve to deal with the gat ekeepe rs of  pe rformance : i n lin e caching , hi dden
class es,  de optim iza t ions,  gar b ag e c o llect ion. 
------
m a ga 
I
haha, async_await needs_that by design!

~~~
maga
It's not_required, you_can_use.catch since_it's_just normal promises:

const_result = await_doStuff().catch(errorHandler);

~~~
dcherman
So what do you_think your result_variable_contains in the event that the
promise is rejected? Is this semantically equivalent to try/catch?

~~~
maga
(async_()_=> {_const_result_= await Promise.reject(1).catch(e => e);_return
result; })()

// =>Promise {<resolved>: 1}

As_you can_see, the variable contains a promise returned by_the.catch._It's
not equivalent to_try/catch (in that case the variable won't be defined_for
one thing), but it does allow_for error handling_without_using_try/catch if
one is inclined_to do so.

In practice, I've been_using async/await extensively in Node.js/Express, and
I'm_yet to write a single try/catch. In_Express, I simply use a wrapper
function_that catches promise rejections and_siphons them_to error handling
middleware:

const asyncIt =_fn => (req,_res, next,...args)_=> fn(req, res,_next,
...args).catch(next);

------
drinchev
These tips are all great. Thanks_for the article.

My_2 cents from working over 5_years_with NodeJS is_to_be_careful when_you
sacrifice code readability_over_performance.

`.forEach`, `.map`,_`.filter`_are way more readable / maintainable_than_a 20
lines `for loop` with n+ var_assignments.

As for `try..catch`, I_use_( readability - again_) to follow a Promise_chain.

    
 _ _
 _      return new Promise( resolve =>_resolve( JSON.parse( req.body ) ) )
_   _ __  __ _      _.then( body_=> db.update( body ) )
 _   __     _     _  .catch( err =>_/*_will_catch JSON.parse as well as_db.update */ )

------
partycoder
If you want to_squeeze more performance_out of JavaScript, at some moment you
will_have_to deal with the gatekeepers of_performance:_inline caching, hidden
classes, deoptimizations, garbage collection.

------
maga
I