Last active
June 3, 2026 12:27
-
-
Save sahilrajput03/cca866fef192e7728d64d983219b9467 to your computer and use it in GitHub Desktop.
try/catch and then/catch (2 June 2026)
This file contains hidden or 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'); | |
| // 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(); |
This file contains hidden or 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'); | |
| // 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