Here is a simple snipped to do an Ajax request
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', true);
req.send(null); // First - send the request
// Then bind for the "result"
req.onreadystatechange = function (e) {
if (req.readyState == 4 && req.status == 200) {
// ...
}
};
What I am doing here:
- Sending the Ajax request.
- And then binding for the result callback.
If you think of this code as multithreaded - it is obvious that you can bind to onreadystatechage
after the request has finished (due to scheduling) and it will never be called.
But in reactor-based code, this will work as expected all times since the reactor will not run until after all my code has finished running for that iteration.
Which is the case in browsers? Has this been documented somewhere?
Ignore that fact that an Ajax request is a slow thing and this might never happen in practice. I'm just giving it as an async example (think websocket if ajax is too slow for you).