Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous programming in javascript (NOT AJAX)

Is it possibly to do things asynchronously in javascript (AJAX aside)? For example, to iterate multiple arrays at the same time. How is it done? A brief example would be nice. Searching for this was hard, due to all the ajax pollution, which is not what I am looking for.

Thanks in advance.

like image 676
aepheus Avatar asked Jul 20 '10 20:07

aepheus


People also ask

Is AJAX the same as asynchronous JavaScript?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

Which methods are asynchronous in JavaScript?

Callbacks, Promises, and Async/Await These are asynchronous functions.

Can we make JavaScript asynchronous?

In the real world, callbacks are most often used with asynchronous functions. A typical example is JavaScript setTimeout() .

Why is JavaScript not asynchronous?

JavaScript is always synchronous and single-threaded.JavaScript is only asynchronous in the sense that it can make, for example, Ajax calls. The Ajax call will stop executing and other code will be able to execute until the call returns (successfully or otherwise), at which point the callback will run synchronously.


2 Answers

Use web Workers. But remember that it is a very new feature and not all browsers are fully supported.

like image 113
Teja Kantamneni Avatar answered Sep 30 '22 05:09

Teja Kantamneni


You could use setTimeout.

setTimeout(function () { iterateArray(array1); reportDone(1); }, 0);
setTimeout(function () { iterateArray(array2); reportDone(2); }, 0);

I'm not sure how concurrent it will be, but it is an asynchronous programming model.

like image 45
Grumdrig Avatar answered Sep 30 '22 06:09

Grumdrig