Last active
May 1, 2024 12:52
-
-
Save up1/76c462894bb9583ce82e8ee52a1a6764 to your computer and use it in GitHub Desktop.
NodeJS with Async/Await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
Return is missing and use const instead of let.