Skip to content

Instantly share code, notes, and snippets.

@sahilrajput03
Last active June 3, 2026 12:27
Show Gist options
  • Select an option

  • Save sahilrajput03/cca866fef192e7728d64d983219b9467 to your computer and use it in GitHub Desktop.

Select an option

Save sahilrajput03/cca866fef192e7728d64d983219b9467 to your computer and use it in GitHub Desktop.
try/catch and then/catch (2 June 2026)
const axios = require('axios');
// Using try/catch
async function main() {
try {
const anecdotesResponse = await axios.get('https://raw.githubusercontent.com/sahilrajput03/loveapi.ml/master/fso/anecdotes.json');
const { anecdotes } = anecdotesResponse.data;
console.log("βœ… ~ anecdotes?", anecdotes);
} catch (e) {
console.log('❌ Error1:', { name: e.name, messag: e.message });
// console.log('Full error2?', e);
} finally {
console.log('πŸŽ‰ (1) I always run in the final.');
}
try {
const myResponse = await axios.get('bad_url_here');
// use myResponse here
} catch (e) {
console.log('❌ Error2:', { name: e.name, messag: e.message });
// console.log('Full error2?', e);
} finally {
console.log('πŸŽ‰ (2) I always run in the final.');
}
}
main();
const axios = require('axios');
// Using then/catch
axios
.get('https://raw.githubusercontent.com/sahilrajput03/loveapi.ml/master/fso/anecdotes.json')
.then((anecdotesResponse) => {
const { anecdotes } = anecdotesResponse.data;
console.log('βœ… ~ anecdotes?', anecdotes);
})
.catch((e) => {
console.log('❌ Error1:', { name: e.name, messag: e.message });
// console.log('Full error1?', e);
})
.finally(() => {
console.log('πŸŽ‰ (1) I always run in the final.');
});
axios
.get('bad_url_here')
.then((myResponse) => {
// use myResponse here
})
.catch((e) => {
console.log('❌ Error2:', { name: e.name, messag: e.message });
// console.log('Full error2?', e);
})
.finally(() => {
console.log('πŸŽ‰ (2) I always run in the final.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment