Hacker News new | past | comments | ask | show | jobs | submit login

The think about using async/await its to write less code, make it much simple and makes also easy to try/catch errors.

The think I don't like about promises its that you need to write a lot of lines that you could avoid with async/await, i.e:

  new Promise(function(success, failure){
    db.fetch('sql...', function(err, data){
      if (err) failure(err)
      else success(data)
    })
  })
VS

  var data = await db.fetch('sql...')
Also if you concatenate with then

  promise.then(function(done, args){
      try {
        db.fetch('sql...', done)
      } catch(e) {}
    })
    .then(function(done, args){
      try {
        db.fetch('sql2... {args}', done)
      } catch(e) {}
    })
    .then....
VS

  try {
    var data1 = await db.fetch('sql1...')
    var data2 = await db.fetch('sql2... {data1}')
  } catch(e) {}
btw its just pseudocode but you get the main idea



To correct this a little, the proper way to catch with promises is .catch, not with a try-catch inside a .then - that is an antipattern. The promise examples as written are not proper promise usage.

I'm also in the boat of confused people of why is async-await requested by a good portion of the js community - wrapping the block within the function in a try-catch seems like a bad idea, a brute force mechanism. IMO this is language bloat, and does not really solve problems we have while introducing a more expensive vector for managing flow (try-catches).

One missed point about the .catch with promises is that it allows you to branch flow discretely - it is not a perfect solution for branching flow (it is awkward when branching due to more than just a binary outcome), but with await, one might end up writing repetitive code for certain flows where one expects common calls to be made downstream in async data fetching flows.


>To correct this a little, the proper way to catch with promises is .catch, not with a try-catch inside a .then - that is an antipattern.

Depends whether you can survive and continue from an exception inside the .then to the next step or not.


Promises catch exceptions and automatically reject: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... . I've never seen a promise not do this - do you have an example where this is not the case?


As I wrote in depends whether you can survive and continue from an exception inside the .then to the next step or not -- not about how the promise would handle an exception, but whether you want it to handle it or you want to survive and handle it yourself.

e.g, naive example:

  .then(() => {
     var foo;
     try {
          foo = getFoo();
     } catch (err) {
          foo = 10; //some default value
     }
     return foo;
  }).then( (foo) => 
etc.

So you might not want to reject automatically inside then if getFoo raises, but recover and return a default value.


That example could be written:

    .then(getFoo)
    .catch(err => 10) //some default value
    .then( (foo) =>


cool, learned something new


How does one add timeouts to fetch? E.g. is db.fetch('sql', done, 1000) possible and how do you know the thing you're trying to fetch has timed out?




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: