Last active
August 28, 2023 09:24
-
-
Save unes/beec36ee89cf12b716b64b5c1bd2c053 to your computer and use it in GitHub Desktop.
Node.js - Javascript Promises and 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
const axiosRequest = require('axios'); | |
axiosRequest | |
.get('http://www.boredapi.com/api/activity/') | |
.then(response => { | |
console.log(`You could ${response.data.activity}`); | |
}) | |
.catch(error => { | |
console.error(`ERROR! ${error}`); | |
}); | |
// by using await inside the async func | |
const axiosRequest = require('axios'); | |
async function getAct() { | |
try { | |
let response = await axiosRequest.get('https://httpstat.us/500'); | |
console.log(`You could ${response.data.activity}`); | |
} catch (error) { | |
console.error(`ERROR: ${error}`); | |
} | |
} | |
getAct(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment