Skip to content

Instantly share code, notes, and snippets.

@mheiber
Last active March 10, 2023 15:49
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 mheiber/d5af532470d524cbfb97707350f51844 to your computer and use it in GitHub Desktop.
Save mheiber/d5af532470d524cbfb97707350f51844 to your computer and use it in GitHub Desktop.
"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