Skip to content

Instantly share code, notes, and snippets.

@unes
Last active August 28, 2023 09:24
Show Gist options
  • Save unes/beec36ee89cf12b716b64b5c1bd2c053 to your computer and use it in GitHub Desktop.
Save unes/beec36ee89cf12b716b64b5c1bd2c053 to your computer and use it in GitHub Desktop.
Node.js - Javascript Promises and Async/Await
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