Asynchronous Functions

Lately i have started liking JavaScript. Undoutedly Node.JS non blocking I/O (asynchronous) operations seemed very fascinating to me.


iDEA

Why not make our own custom asynchronous functions. If you get your hands dirty in node.js you will see that we work around with non blocking functions a lot. But have never built one.

Saying that the harsh truth is you can not built you own custom asynchronous function. JavaScript does not provide any API to build one.

But there is a simple work around. We can use existing javascript asynchronous functions and build custom asynchronous function.

WorkAround

//Custom asynchronous function
var databaseOperation = function(query, callback) {
setTimeout(function() {
var v = ‘JavaScript ‘ + query;
callback(v)
}, 0);
}

above function internally calls setTimeout which is javascripts built in asynchronous function. But have set the time out to Zero, which will execute the inner function block instantly, nullyfying the affect of timeout. Thus making databaseOperation() non blocking.

Well you can use any built in Async functions like:

- setInterval
- setTimeout
- requestAnimationFrame
- XMLHttpRequest
- WebSocket
- Worker
- onload
many more.

Enjoy! Thanks for reading so patiently.