Skip to content

Instantly share code, notes, and snippets.

@caasi
Created August 18, 2018 16:37
Show Gist options
  • Save caasi/82b746a982a99469e2f8c2a75205df53 to your computer and use it in GitHub Desktop.
Save caasi/82b746a982a99469e2f8c2a75205df53 to your computer and use it in GitHub Desktop.
function func0() { throw new Error('oops'); }
function func1() { return console.log('foo') || Promise.resolve('bar'); }
async function main() {
try {
await func0();
await func1();
} catch (e) {
console.error(e);
}
}
main();
@caasi
Copy link
Author

caasi commented Aug 18, 2018

如果堅持兩個 function 都一定要跑:

function func0() {
  console.log('start func0');
  return new Promise((resolve, reject) => {
    window.setTimeout(() => reject(new Error('oops')), 1000);
  });
}

function func1() {
  console.log('start func1');
  Promise.resolve('bar');
}

async function main() {
  try {
    const p0 = func0();
    const p1 = func1();
    await p0;
    await p1;
  } catch (e) {
    console.error(e);
  }
}

main();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment