Last active
March 10, 2023 15:49
-
-
Save mheiber/d5af532470d524cbfb97707350f51844 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
function fetch(uri) { | |
throw new Error("synchronous"); | |
} | |
function sync() { | |
return fetch('http://stuff'); | |
} | |
async function async1() { | |
return await fetch('http://stuff'); | |
} | |
function async2() { | |
return new Promise((resolve, _reject) => | |
resolve(fetch('http://stuff')); | |
); | |
} | |
async function test(f) { | |
try { | |
f() | |
.catch(err => console.log(`caught rejected promise from ${f.name}`)); | |
} catch (_) { | |
console.log(`caught synchronous error from ${f.name}`); | |
} | |
} | |
[sync, async1, async2].forEach(f => test(f)); | |
/* Output: | |
caught synchronous error from sync | |
caught rejected promise from async1 | |
caught rejected promise from async2 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment