Created
December 8, 2022 18:04
-
-
Save nathansmith/33a7bd7b9af305f27a50017b2a015d83 to your computer and use it in GitHub Desktop.
Promise snippets I put together, to share with coworkers.
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
// ============ | |
// Helper: log. | |
// ============ | |
const log = (error) => { | |
console.log(error?.message || error); | |
} | |
// =============== | |
// Promise reject. | |
// =============== | |
Promise.reject('REJECT').catch(log); | |
// ====================== | |
// Promise reject: await. | |
// ====================== | |
try { | |
await Promise.all([ | |
Promise.reject('REJECT'), | |
Promise.resolve('RESOLVE') | |
]); | |
} catch (error) { | |
log(error); | |
} | |
// ============= | |
// Fetch reject. | |
// ============= | |
fetch('https://localhost').catch(log); | |
// ==================== | |
// Fetch reject: await. | |
// ==================== | |
try { | |
await fetch('https://localhost'); | |
} catch (error) { | |
log(error); | |
} | |
// ============================= | |
// Short circuit following code. | |
// ============================= | |
(async () => { | |
log('BEFORE'); | |
try { | |
await Promise.reject('REJECT'); | |
// Never logged. | |
log('AFTER'); | |
} catch (error) { | |
log(error); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment