Why does WebSocket event listeners work if they are registered after events are fired?
问题 According to MDN docs, the WebSocket event listeners are registered after the connection is created, and the process is sequential: // Create WebSocket connection. const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', function (event) { console.log('ws connected'); }); // Listen for messages socket.addEventListener('message', function (event) { console.log('Message from server ', event.data); }); How does this work if events start firing