DEV Community

loading...

Discussion on: JavaScript Fetch API and using Async/Await

Collapse
delbetu profile image
M Bellucci

An alternative solution for getUserAsync using then:

async function getUserAsync(name) 
{
  await fetch(`https://api.github.com/users/${name}`).then(async (response)=> {
  return await response.json()
}
Enter fullscreen mode Exit fullscreen mode

Agree with you that your solution is more readable.
Actually, does it make sense to use async/await inside then?

(I'm getting back to javascript after a few years and this Promise handling is new for me)

Collapse
totalamd profile image
totalamd

Seems like too many async/await for me but I'm not sure which one can be removed right now.
At least last await is redundant: jakearchibald.com/2017/await-vs-re...

Collapse
shoupn profile image
Nick Shoup Author

Actually, I think your implementation is a bit neater, as it keeps the async and await handling inside the function. Nice!

Collapse
kyawmini profile image
kyawshwe

Yes bro

Collapse
mistval profile image
Randall

That won't work, but this is a good way to keep things brief with fetch, and doesn't require the extraneous res variable:

const data = await fetch(`https://api.github.com/users/${name}`).then(r => r.json());
Enter fullscreen mode Exit fullscreen mode