concept XMLHttpRequest in category chaos engineering

This is an excerpt from Manning's book Chaos Engineering.
XMLHttpRequest built-in class (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest)
The Events section of the XMLHttpRequest documentation lists all the events that an instance of XMLHttpRequest can dispatch. One event looks particularly promising - the error event, that’s described like this:
error Fired when the request encountered an error. Also available via the onerror property.It’s a legal event that can be emitted by an instance of XMLHttpRequest, and it’s one that should be handled gracefully by the pgweb application, which makes it a good candidate for our experiment!
Now you’ve now got all the elements, let’s assemble them into a code snippet. Just like before, you’ll need to override the window.XMLHttpRequest.prototype.send but keep a reference to the original method. You’ll need a counter to keep track of which call is “every other one.” And you can use the dispatchEvent method directly on the XMLHttpRequest instance to dispatch a new event that you can create with a simple new Event('timeout'). Finally, you want to either dispatch the event or do nothing (just call the original method), based on the value of the counter. You can see a snippet doing just that in listing 9.2.