How .onload handler in XMLHttpRequest is called when finish load request data

I dont understand why .onload is called when myRequest finish get data from url. And how? can i make a object similar to XMLHttpRequest. For example: a object Vehicle has some properties like : name, wheels,toSomewhere … and it also has onArrival handler. we have Vehicle.toSomeWhere(start,destination); // add direction. and then, when it arrives .onArrival is called and we can passed in a anonymous function like .onload handler in XHR. Sorry for my english

Well the how it “ajax” works is based on an event loop and what I believe are call web workers. In a nutshell your get/post/request is handed off to a separate part of javascript that will wait on the ajax response. the JavaScript event loop knows it is waiting on this response, but if it does not return, check once per loop, then it continues to work on other code.

This is called non-blocking execution and it is very important for JavaScript to function within the limited environment of a web browser.

There is a great video which will better explain this in-works of JS then I ever will.

for your onload question I guess you mean the load event that fires when the resource is done loading.

In a nut shell it is so you get the entirety of the response from your request and it relates to how information is passed over the internet. For the most part data is split into smaller chunk called data packets. You would need to read up on networking if you really want the specifics.

You may want to read up on serial communication and streams.

thanks for your reply. i think im digging in too deep:grin: