Skip to content

Instantly share code, notes, and snippets.

@WoZ
Last active June 21, 2019 15:12
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 WoZ/50e32b7855082dff4a3c5fae20924e44 to your computer and use it in GitHub Desktop.
Save WoZ/50e32b7855082dff4a3c5fae20924e44 to your computer and use it in GitHub Desktop.
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