Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX implementation in single-threaded Javascript

How are AJAX requests made asynchronous if Javascript is not multi-threaded?

Is the implementation browser specific?

like image 704
ainokna Avatar asked Oct 08 '12 16:10

ainokna


People also ask

Is AJAX single threaded?

So yes, it uses additional threads internally.

How JavaScript is single threaded language?

In the context of programming, Parallelism is the utilization of multiple threads in an operating system. Routines are able to run at the same time regardless of execution order. JavaScript, however, is single threaded and only one line of code can be executed at any given time.

Can we write AJAX in JavaScript?

Ajax is a programming concept. Below are some ways to make Ajax call in JavaScript. Approach 1: In this approach, we will use the XMLHttpRequest object to make Ajax call. The XMLHttpRequest() method which create XMLHttpRequest object which is used to make request with server.

How does js implement asynchronous functionality when it is a single thread?

Javascript is a single threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece code before moving onto the next.


1 Answers

The browser execution model is based on the concept of an "event loop". There's only one thread servicing events (slight oversimplification). When an event happens, handlers are called in sequence.

Ajax is just a mechanism that causes certain events. Setting up an HTTP request is synchronous, but just setting it up. The browser responds to the network communications representing the return data from the server by triggering events when that happens.

Modern browsers are somewhat more complicated in that each window may have its own process (or some other system-level "thread" construct, to be general). Also, the new "web worker" feature allows separate thread-like compartments to run concurrently.

like image 51
Pointy Avatar answered Oct 30 '22 01:10

Pointy