3 ms·
That won't work because if thing1() throws you usually don't want to process to thing2 Take this for example: const response = await fetch('./api.json');
by msoad 10y ago
That won't work because if thing1() throws you usually don't want to process to thing2
Take this for example:
const response = await fetch('./api.json');
try {
const json = await response.json();
console.log('YAY! JSON!', json);
} catch (jsonParseError) {
alert('damn it!');
}
} catch(fetchError) {
console.warn('this is not cool');
}
- WorldMaker 10y agoOkay, that's a fair counter-argument. My gut feeling is that's possibly one catch to many and as much as possible I'd want to try to merge the two catches/unifying the error response logic. Another idea is that you could reformat to a "parade" try/catch by adding returns to the catch: try { const response = await fetch('./api.json') } catch (fetchError) { console.warn('this is not cool') return // Exit } try { const json = await response.json(); console.log('YAY! JSON!', json); } catch (jsonParseError) { alert('damn it!'); } Obviously you'd need to move any code you'd want to run regardless of error up into the parent function or out into a wrapper function.