Skip to content

Instantly share code, notes, and snippets.

@WoZ
Last active June 21, 2019 15:12
Embed
What would you like to do?
Simplified example of a data loss
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