Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Created December 8, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/33a7bd7b9af305f27a50017b2a015d83 to your computer and use it in GitHub Desktop.
Save nathansmith/33a7bd7b9af305f27a50017b2a015d83 to your computer and use it in GitHub Desktop.
Promise snippets I put together, to share with coworkers.
// ============
// 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