title: XMLHttpRequest onreadystatechange never fired for readyState 0
body: <p>I set up the usual XMLHttpRequest and everything works fine so far, I can retrieve data and readyState changes and does all sorts of things.</p>
  
  <p>One thing that doesn't happen though is that readyState ever triggers the onreadystatechange() function when it reaches 0.</p>
  
  <p>Now I'm wondering... why?</p>
  
  <p>I looked into the <a href="https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/onreadystatechange" rel="nofollow noreferrer">XMLHttpRequest MDN entry</a> but there's no mention of why onreadystatechange should NOT fire when readyState reaches 0. I also found no other information on the topic...</p>
  
  <p>Snippet:</p>
  
  <pre><code>// Inside a class, this = class instance
  this.socket = new XMLHttpRequest();
  
  // For easy access of this class instance in onreadystatechange
  var client = this;
  
  // Write current readyState to console
  this.socket.onreadystatechange = function ()
  {
      // Adding this just to verify there are no errors in if block
      console.log("readyState: "+client.socket.readyState);
  
      if(client.socket.readyState === 0)
      {
          console.log("readyState is 0");
      }
      else
      {
          console.log("readyState is "+client.socket.readyState);
      }
  }
  </code></pre>
  
  <p>When the XMLHttpRequest socket is done processing the request, the XMLHttpRequest instance will have the value readyState===0, at least Firefox' JS console tells me that.</p>
  
  <p>After doing a request, the JS console lists the log messages for readyStates 1-4 correctly, but no message for readyState 0, not before the request and not afterwards.</p>
  
  <p>In theory, when the readyState changes from 4 back to 0, shouldn't the onreadystatechange() function be triggered? Checked this on macOS with current FF and Safari, as well as on Win with IE11, the same behaviour everywhere, onreadystatechange() never fires for readyState 0.</p>
  
  <p>Is XMLHttpRequest intentionally not doing that, by design, or am I doing something wrong? Grateful for any helpful input. :)</p>
  
  <p>(Please no "use jQuery" comments or similar, thx.)</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>Turns out the readyState stays at 4 forever after a successful or failed request is completed, verified in FF, IE, Safari.</p>
  
  <p>However, the readyState will return from > 0 to 0 if an ongoing request is aborted. So a readystatechange does happen, but the onreadystatechange is not fired, verified this in FF and Safari.</p>
  
  <p><strong>UPDATE 3</strong>:</p>
  
  <p>According to the <a href="https://xhr.spec.whatwg.org/#the-abort()-method" rel="nofollow noreferrer">XMLHttpRequest spec</a>, when XHR is in readyState 3 (receiving) and aborted, the XHR should set readyState to 4 (done) with an onreadystatechange event, issue a network error and then set readyState to 0 (unsent) with <em>no</em> onreadystatechange event.</p>
  
  <p>But when I abort an XHR that is in readyState 3 (receiving), it will first set readyState to 4 (done) with an onreadystatechange event and HTTP status 200 (OK), then it will trigger onabort and onloadend and reset readyState to 0 (unsent) ... but at no point is an onerror Event triggered.</p>
  
  <p>Very confusing.</p>