Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are all javascript callbacks asynchronous? If not, how do I know which are?

I'm curious as to whether all javascript callbacks are asynchronous, or whether that is the case only in certain situations. Also, I'm sure what makes javascript code asynchronous (or ways to use asynchronous javascript) differ between the browser and nodejs, so I'd like to know in each situation what constitutes real asynchronous javascript.

I'm under the impression that in the following scenarion, I'm not actually writing asynchronous code.

function addOne(value){   value = value + 1;   return value; }  function simpleMap(values, callback){   for(i = 0; i < values.length; i++){     val = values[i];     val = callback(val);     values[i] = val;   }   return values; }  newValues = simpleMap([1,2,3], addOne); 

However, for example, I know that jQuery's AJAX functions are truly asynchronous (not taking in to account the promises which are now available). What is it that make jQuery's AJAX asynchronous? Is it as simple that it involves XHR requests, and in the browser, all XHR requests are asynchronous?

I have the same question for the nodejs environment. Can something in node only be asynchronous if it involves something like file i/o, process.nextTick, setTimeout, or setInterval? Why when I do something like a database call with mongodb/mongoose, is that asynchronous? What's going on behind the scenes that's making it so?

Are asynchronous "situations" predetermined by the environment? Or is there some way to make one's own function truly asynchronous without leveraging very specific functions of the environment (such as xhr, file io in node, process.nexttick, etc)?

like image 857
jacheson Avatar asked Sep 29 '13 21:09

jacheson


People also ask

Are all callbacks asynchronous in JavaScript?

The function that takes another function as an argument is called a higher-order function. According to this definition, any function can become a callback function if it is passed as an argument. Callbacks are not asynchronous by nature, but can be used for asynchronous purposes.

Is everything in JavaScript asynchronous?

JavaScript is a single-threaded, non-blocking, asynchronous, concurrent programming language with lots of flexibility.

Are JavaScript callbacks synchronous?

Summary. The callback is a function that's accepted as an argument and executed by another function (the higher-order function). There are 2 kinds of callback functions: synchronous and asynchronous. The synchronous callbacks are executed at the same time as the higher-order function that uses the callback.

How does JavaScript know when a function is asynchronous?

To check if a function is async, access the constructor.name property on the function and check if the value is equal to AsyncFunction , e.g. myFunction.constructor.name === 'AsyncFunction' . If the equality check returns true , then the function is async.


1 Answers

I'm curious as to whether all javascript callbacks are asynchronous

No. For instance, the callback used by Array#sort is not asynchronous, nor is the one used by String#replace.

The only way you know whether a callback is asynchronous is from its documentation. Typically, ones involving requests for external resources (ajax calls, for instance) are asynchronous, and others may or may not be.

However, for example, I know that jQuery's AJAX functions are truly asynchronous...

Not necessarily, as currently jQuery still has the async flag which you can set false to force a synchronous request. (It's not a good idea, and they're going to remove that, but you can. jQuery passes the flag to the underlying browser object which provides the synchronous/asynchronous behavior.)

What is it that make jQuery's AJAX asynchronous?

The browser. jQuery's ajax calls use the XMLHttpRequest object (or in certain situations, a script element), which defaults to asynchronous operation provided by the browser.

Or is there some way to make one's own function truly asynchronous without leveraging very specific functions of the environment...

Until recently, no. Up through the 5th edition specification, JavaScript the language was basically silent on the entire concept of threads and asynchronicity; it was only when you got into environments that it came up. The only way to make something asynchronous was to use a host-provided function, such as nextTick (or any of the various operations that completes asynchronously) on NodeJS or setTimeout on browsers.

In the ECMAScript 6th edition specification in June 2015, they introduced promises into the language. The callbacks hooked up to an ES6 promise via then and such are always invoked asynchronously (even if the promise is already settled when the callback is attached), and so JavaScript has asynchronicity at a language level now. So if you implement your function so that it returns a promise rather than accepting a callback, you'll know that the then callbacks hooked up to it will be triggered asynchronously.

like image 124
T.J. Crowder Avatar answered Sep 24 '22 05:09

T.J. Crowder