Simplified example of a data loss
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
const {EventEmitter} = require('events'); | |
const ee = new EventEmitter(); | |
function waitForMessage() { | |
return new Promise(resolve => { | |
ee.once('message', msg => { | |
console.log('waitForMessage. Message is received', msg); | |
resolve(); | |
console.log('waitForMessage. Ooops, resolve will run in microtask. not immediately'); | |
}); | |
}); | |
} | |
setTimeout(() => { | |
console.log('emitting message #1'); | |
ee.emit('message', 'm1'); | |
console.log('emitting message #2'); | |
ee.emit('message', 'm2'); | |
}, 1000); | |
(async () => { | |
console.log('start of await #1'); | |
await waitForMessage(); | |
console.log('end of await #1'); | |
console.log('start of await #2'); | |
await waitForMessage(); | |
console.log('end of await #2'); | |
})(); | |
setInterval(() => {}, 1000); | |
/* | |
start of await #1 | |
emitting message #1 | |
waitForMessage. Message is received m1 | |
waitForMessage. Ooops, resolve will run in microtask. not immediately | |
emitting message #2 | |
end of await #1 | |
start of await #2 | |
// process hanged out | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment