Last active
September 12, 2018 14:54
-
-
Save rbaumbach/601d57c570239f08a28cf5b3d0de4f21 to your computer and use it in GitHub Desktop.
Having "fun" with axios and handling
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 axios = require('axios'); | |
// Note: axios.get() returns a promise | |
const getPhoto = () => { | |
return axios.get('https://jsonplaceholder.typicode.com/photos/1'); | |
}; | |
console.log('Return of getPhoto:', getPhoto()); | |
getPhoto().then((response) => { | |
console.log('imageURL:', response.data.url); | |
}); | |
const getPhotoAsync = async () => { | |
const response = await axios.get('https://jsonplaceholder.typicode.com/photos/1'); | |
return response; | |
}; | |
console.log('Return of getPhotoAsync:', getPhotoAsync()); | |
getPhotoAsync() | |
.then((response) => { | |
console.log('imageURL async:', response.data.url); | |
}); | |
console.log('EL FIN'); | |
/* | |
Return of getPhoto: Promise { <pending> } | |
Return of getPhotoAsync: Promise { <pending> } | |
EL FIN | |
imageURL async: https://via.placeholder.com/600/92c952 | |
imageURL: https://via.placeholder.com/600/92c952 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.fillmurray.com/500/400