Reputation: 91669
Node.js is stated to be asynchronous, event-driven, non-blocking I/O, but how can I identify if my script is asynchronous? Or non-blocking?
Upvotes: 3
Views: 1309
Reputation: 5936
Read the code ;) It's often pretty obvious when libraries are asynchronous as it often facilitates continuation- or message passing-style code. Oftentimes you can make a call block on purpose and see how it reacts. Or better: profile your code.
I don't know of any tool that can answer true or false to "is the input code async or sync" :)
Or you could share the code or some more context :) ?
EDIT:
Could you give me an example of blocking or non-asynchronous code? – hexacyanide
This question has a discussion on this: When is JavaScript synchronous?
Synchronous code would be something like this:
console.log("first");
console.log("second");
console.log("third");
These three lines of code run synchronously - first, second then third line.
This code on the contrary, runs asynchronously:
function f() {
alert("first");
}
function g() {
alert("second");
}
// defer execution for 1000 ms
setTimeout(f, 1000);
// defer execution for 500 ms
setTimeout(g, 500);
// run immediately
alert("third");
You'll see the "third"-alert first, then "second" and lastly "first". This is a very contrived example, but you see the callbacks (f and g) and how the flow is fundamentally different from the synchronous code.
You can read synchronous code and mostly things will happen like you read them, where-as in asynchronous code, events will fire or callbacks get called, and the execution path may not be as obvious as with synchronous blocking code.
Node.js is an example of asynchronous code, where most function calls take a set of callback parameters for success, failure and whatever else might apply.
EDIT2:
This link has some Javascript examples with the same code written sync/async-style: Asynchronous Code Design with Node.js
Upvotes: 2
Reputation: 56477
There is a list of functions which are asynchronous, including:
process.nextTick( ... )
setTimeout( ... )
setInterval( ... )
From File System module:
var fs = require( 'fs' );
fs.rename( ... )
fs.stat( ... )
fs.readFile( ... )
fs.writeFile( ... )
fs...
many more for fs
, basically asynchronous have their synchronous versions with "Sync" suffix at the end ( synchronous versions should be avoided though ).
There are many many more examples. EventEmitters can be considered asynchronous ( for example HTTP servers or streams ), although technically they aren't ( they depend on asynchronous operations ).
Basically almost everything which accepts callback
as a parameter is asynchronous ( or depends on asynchronous operation ), for example:
process.nextTick( function( ) { // <--- the callback
console.log( 'nextTick' );
});
console.log( 'test' ); // <--- will fire before the callback, i.e. asynchronouos
Keep in mind that this is obviously not a rule, notable example:
var arr = [ 1, 2, 3 ];
arr.forEach( function(el) { // <--- the callback
console.log( el );
});
console.log( 'test' ); // <--- will fire after the callback, i.e. synchronous
So synchronous code is a code which fires line after a line and order of lines matters. The asynchronous code is the opposite: the order of lines ( almost ) does not matter. In the example with process.nextTick
you can change the order of process.nextTick
and console.log('test')
and the result will still be the same.
Read the documentation and try to memorize all basic asynchronous functions.
And one more advice: the best way to learn is learn by doing. ;-)
Upvotes: 2