ainokna
ainokna

Reputation: 865

AJAX implementation in single-threaded Javascript

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

Is the implementation browser specific?

Upvotes: 7

Views: 1151

Answers (1)

Pointy
Pointy

Reputation: 413717

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.

Upvotes: 8

Related Questions