Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Wilbur Powery
Wilbur Powery

Posted on

     

Async + Await refactor, which do you like best?

Async + Await is sooo good once you get the hang of it. Just refactored this piece of code and removed a lot of indentation levels. Removing indentation levels is always good.#javascript#es6pic.twitter.com/zfFEDyJU0B

— Wilbur Powery (@wilburpowery)May 18, 2018

Top comments(12)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
avalander profile image
Avalander
"Biography is irrelevant" - Seven of nine (probably) if she was asked that question.
  • Joined
• Edited on• Edited

I don't think thatasync/await is a bad thing, but I like that Promises force you to think what parts of your code depend on which asynchronous data and to organise it accordingly. Async/await allows you to write asynchronous code like it is synchronous and I have mixed feelings about this.

It's worth mentioning that I don't use either, I usually useFluture because it doesn't automagically catch all and every error in the rejection branch, which you can't achieve with async/await or Promises. But the Fluture API is quite similar to a Promise and I kind of like it.

Also, notice that if the goal is to remove indentation, there is no reason why the lastthen of the example on the left can't be rewritten as

.then(response=>{this.updateEventValues()returnthis.Alert.success('Se ha actualizado el evento')).then(()=>this.clearEventAndClose())

Which has the additional benefit that if you need tocatch any rejection, you only need to do it once.

CollapseExpand
 
ben profile image
Ben Halpern
A Canadian software developer who thinks he’s funny.
  • Email
  • Location
    NY
  • Education
    Mount Allison University
  • Pronouns
    He/him
  • Work
    Co-founder at Forem
  • Joined

I agree with this logic

CollapseExpand
 
erebos-manannan profile image
Erebos Manannán
  • Location
    Estonia
  • Education
    Autodidact
  • Joined

Neither is very good, as you're doing nothing to handle errors.

One requires.catch() in several places, the other atry { ... } catch (...) { ... } block or several.

Overall theawait syntax is much easier to read and follow the logic, but if you have to put atry..catch around every one it quickly becomes a mess too.

CollapseExpand
 
qm3ster profile image
Mihail Malo
  • Joined

I often do the following:

awaitfallible().catch(errorHandler)

It allows me to pipe/sidechain my errors where they truly belong, instead of breaking up my happy path logic with inline implementation of the catch block OR lambda.
If I need to abort the current function, the handler will just rethrow.

CollapseExpand
 
wilburpowery profile image
Wilbur Powery
  • Joined

Yeah, I agree. As with everything, you should choose carefully 😄

CollapseExpand
 
tailcall profile image
Maria Zaitseva
Software developer from Novosibirsk, Russia. Likes pretty stuff
  • Location
    Novosibirsk, Russia
  • Education
    Master in Electrical Engineering
  • Pronouns
    she/her
  • Work
    iOS developer at Novotelecom
  • Joined

I love async/await and and the same time I despise try/catch, so I tend to use .catch() method for error handling. Just looks neater. I wish I could do all exception handling that way.

CollapseExpand
 
xtofl profile image
xtofl
C++, python software engineer with a passion for design that improves teamwork and maintainability.
  • Location
    Belgium
  • Pronouns
    he/him/his
  • Joined

i think, as a matter of fact, that youcan do all error handling this way. The functional programming folks found that out and called the resulting pattern a Monad. Take a look atm.youtube.com/watch?v=Mw_Jnn_Y5iA#

CollapseExpand
 
buinauskas profile image
Evaldas Buinauskas
  • Location
    Lithuania
  • Education
    Bachelor of Computer Science
  • Work
    Search Engineer at Vinted
  • Joined

This is totally out of topic, but what font is this?

CollapseExpand
 
wilburpowery profile image
Wilbur Powery
  • Joined

Operator Mono 😊

CollapseExpand
 
qm3ster profile image
Mihail Malo
  • Joined

HAHAHA they asked you this on twitter as well :v

CollapseExpand
 
qm3ster profile image
Mihail Malo
  • Joined

I think the async/await is a clear winner here, since these were 4 sequential operations, 2 of which just needed to yield yet still block the progress of this function, and cancel the function if they throw.
It's the textbook happy case for async/await.

I do agree with@avalander's refactor though, which gets you closer to this without the async syntax.

CollapseExpand
 
p250 profile image
01101001
  • Joined

i like async and await. Especially await :))

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromWilbur Powery

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp