Skip to content

Instantly share code, notes, and snippets.

@rbaumbach
Last active September 12, 2018 14:54
Show Gist options
  • Save rbaumbach/601d57c570239f08a28cf5b3d0de4f21 to your computer and use it in GitHub Desktop.
Save rbaumbach/601d57c570239f08a28cf5b3d0de4f21 to your computer and use it in GitHub Desktop.
Having "fun" with axios and handling
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
*/
@gordonhgraham
Copy link

update line six, return axios.get('http://www.fillmurray.com/');

@rbaumbach
Copy link
Author

Give me an actual url to an image you love and I'l update it. Ex: http://www.fillmurray.com/500/500

@gordonhgraham
Copy link

http://www.fillmurray.com/500/400

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