Skip to content

Instantly share code, notes, and snippets.

@up1
Last active February 24, 2021 10:39
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save up1/76c462894bb9583ce82e8ee52a1a6764 to your computer and use it in GitHub Desktop.
Save up1/76c462894bb9583ce82e8ee52a1a6764 to your computer and use it in GitHub Desktop.
NodeJS with Async/Await
var fetch = require('node-fetch')
async function getDataFromAPI() {
let response = await fetch("https://api.github.com/users/up1")
let data = await response.json()
console.log(JSON.stringify(data, null, "\t"))
}
getDataFromAPI()
var fetch = require('node-fetch')
function getDataFromAPI() {
return fetch("https://api.github.com/users/up1")
.then(response => response.json())
.then(data => console.log(JSON.stringify(data, null, "\t")))
}
getDataFromAPI()
@syed-ahmad
Copy link

Nice one!

@aarkerio
Copy link

¡Muchas gracias!

@Skaronator
Copy link

Return is missing and use const instead of let.

@zzal
Copy link

zzal commented Jul 31, 2019

Should await fetch and await response be enclosed inside try catch statement in order to get the reject from Promise?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment