How to deal with race conditions in event listeners and shared state?
问题 I have 2 event listeners that operate on the same shared data/state. For instance: let sharedState = { username: 'Bob', isOnline: false, }; emitter.on('friendStatus', (status) => { sharedState.isOnline = status.isOnline; }); emitter.on('friendData', (friend) => { if (sharedState.isOnline) { sharedState.username = friend.username; } }); My problem is that these events are emitted at any order. The friendData event might come in before the friendStatus . But friendData does something with the