I think you can be even more concise and completely ditch the use of the promise.then method by using this example (inspired by yours, slightly modified so it can be easily tested):
I think there is something to be said about how a .then reads a bit better, but this can be argued both ways. Either example takes advantage of the language to do one thing: fetch data.
In most scenarios, you would have a helper function anyway, which is essentially what you wrote (I would just rename it to something like fetchUsers()).
Yeah you'll want to throw your own errors if you're using fetch and want to catch any errors. Something to be aware of otherwise most of your 'errors' will end up in your .then
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Sunny, very interesting article. Thanks!
I especially loved this one-liner:
I think you can be even more concise and completely ditch the use of the
promise.then
method by using this example (inspired by yours, slightly modified so it can be easily tested):This is using two
await
keyword, but with parenthesis to control the order of theawait
flow.I also added a simple catch to handle errors (even if I'm not doing interesting stuff with it, yet).
What do you think of that? Is it better or worse? Let me know!
What do you think about this approach
medium.com/@dimpapadim3/asynchrono...
I like it! Even if I personally think that this API could be sexier but I'm a big fan of FP and its concept (especially in Elm, Haskell or Rust).
Definitely more concise!
I think there is something to be said about how a
.then
reads a bit better, but this can be argued both ways. Either example takes advantage of the language to do one thing: fetch data.In most scenarios, you would have a helper function anyway, which is essentially what you wrote (I would just rename it to something like
fetchUsers()
).Just wanted to add in that fetch doesn't throw errors or reject so .catch won't fire there. You need to throw your own errors with fetch.
From the documentation, it is stated that
So I guess this is the case when the fetch method can throw an error.
Yeah you'll want to throw your own errors if you're using fetch and want to catch any errors. Something to be aware of otherwise most of your 'errors' will end up in your .then