Skip to content

Instantly share code, notes, and snippets.

@phihag
Created December 20, 2018 13:52
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 phihag/15639b363425c3cf04a7c481c21dbb84 to your computer and use it in GitHub Desktop.
Save phihag/15639b363425c3cf04a7c481c21dbb84 to your computer and use it in GitHub Desktop.
// mock promises used below
const wait = async (ms) => await new Promise(resolve => setTimeout(resolve, ms));
const send_request1 = () => wait(300), send_request2 = () => wait(200);
async function get_email() {
await wait(Math.random() * 1000);
if (Math.random() > 0.5) throw new Error('failure');
return {subject: 'foobar'};
}
const assert = require('assert');
async function main() {
// THIS CODE IS WRONG - will cause PromiseRejectionHandledWarning!
const email_promise = get_email();
await send_request1();
await send_request2();
const email = await email_promise;
assert.equal(email.subject, 'foobar');
};
(async () => {
try {
await main();
} catch(e) {
console.log('main error: ' + e.stack);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment