Created
December 20, 2018 13:52
-
-
Save phihag/15639b363425c3cf04a7c481c21dbb84 to your computer and use it in GitHub Desktop.
Test case for https://stackoverflow.com/q/53869592/35070
This file contains hidden or 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
// 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