title: How to handle all WebSocket errors without a race?
body: <p>When I run code</p>
  <pre><code>var ws = new WebSocket(&quot;wss://mydomain.example/socket/service&quot;);
  ws.addEventListener(&quot;error&quot;, function (error)
  {
    console.error(&quot;Got error=&quot;, error);
  });
  </code></pre>
  <p><strong>Is it possible that the WebSocket connection fails (emit <code>error</code>) before I can attach the event listener for the <code>error</code> event?</strong></p>
  <p>Looking at the documentation <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket</a> I cannot see this detail documented anywhere.</p>
  <p>According to the <a href="https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket" rel="nofollow noreferrer">WHATWG spec</a> it seems that the constructor should run the request <em>in parallel</em> – is there a guarantee that I can attach the error listener before any possible errors can raise?</p>