Hi,
I was wondering when a received ajax response gets executed. After searching I read about the Event loop, but I am not sure if I interpreted it correctly.

Is it like this?:
- Start execution
- Ajax request is sent
- proceeds execution
- Ajax reply is received and buffered as a message
- executing untill there is nothing to execute
- Any triggered events are buffered in the message queue aswell.
- nothing to execute so read next message and execute the associated function.

Does this mean that when you send an ajax request and then before the ajax response is received 1000 interval triggers and 1000 click events have occured that the ajax response will be handled after the 1000 interval triggers and 1000 click event messages have been handled?

I did a small test to test this and this seems to be the case, but i'd like to be sure and to know if there are exceptions or tricky things I should know about.

Thanks!
Stefan 1

11-07-2013, 02:34 PM

stefan1

Hmm, and I guess with nested ajax requests you still can get a sort of race conditions right?

Example:

Code:

this.streetName = "";
this.houseNumber= "";
this.loadStreetname( function(){
this.loadHousenumber( function(){
console.log(streetName + " " + houseNumber );
}
}, this ) );
// the loadStreetname and loadHousenumber methods both make ajax requests
// They set the streetName and houseNumber variables when the response is received.
// The functions passed as parameter are called on completion.

The streetname may have been changed at the time the house number response is received and thus a wrong address may be printed. Offcourse this can be avoided by loading the entire address in one request, but I guess this is not always practical in other situations.

In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?

11-07-2013, 05:27 PM

sunfighter

Quote:

In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?

The streetname may have been changed at the time the house number response is received and thus a wrong address may be printed. Offcourse this can be avoided by loading the entire address in one request, but I guess this is not always practical in other situations.

In multithreaded languages you have synchronization methods like locks. How are these things handled in Javascript? Just careful programming?

you can load them in two sequential async requests as well. if somethings changed on the back between those two, yeah, you'll be out of sync. But, those IOs happen just as fast sync as async, so even if you had used sync requests to load one after the other, you'd still be out of order. the server doesn't know the difference between a sync and async ajax call.

this is the same exact reason why it's better to join your SQL tables and then SELECT them, rather than use a loop of queries resulting from an up-front SELECT. in order to reduce the risk of conflict, you want as whole of a snapshot as you need. same for ajax.

being single-threaded gives JS a huge advantage in terms of program simplicity.
using asyc IO means that most of the real CPU work happens off-thread; only the JS response handling/templating is done on that single JS thread; and it's usually 99.99% idle...

of course, if you like threads, you can use them, even with ajax, even sync, without blocking your UI thread, using a webworker; new Worker(scriptUrl)...

11-08-2013, 11:34 PM

stefan1

Ok so basically your ownly option for making sure that data that should form a consistent snapshot is to be retrieved in one go from the server.

Another option would be to have some kind of lock like below but this doesn't really seem like an appropriate solution. Its not efficient and if you're not careful you may "forget" to unlock it.