Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Asynchronous Function *Definition*

Please, just to clear something up in my head...

I am used to writing using asynchronous functions in libraries, but how do I write my own?

To illustrate my question I have made up a module called 'MadMathz'

I am aware that the following is an example usage of an asynchronous function :

//load module
var mM = require('./MadMathz');

//perform a function
mM.async_function_addthree(12, function(result) {
  console.log(result)
});

//do something straight afterwards
console.log('Logging this straight after instead of waiting for computation');

I'm aware that the second argument to the function is the callback, but how is a function such as 'async_function_addthree' defined? Let's say for the purpose of this example that async_function_addthree just adds 3 to the first argument.

I just confused myself so much trying to write what I thought it might be. Am I totally on the wrong track? I feel like I am close to getting there with node.js but this needs clearing up.

Please explain rather than linking me somewhere if possible. I'm sure others will benefit from this question.

like image 908
Adam Waite Avatar asked Oct 11 '12 09:10

Adam Waite


People also ask

What is async function in Node JS?

The asynchronous function can be written in Node.js using ‘async’ preceding the function name. The asynchronous function returns implicit Promise as a result. The async function helps to write promise-based code asynchronously via the event-loop. Async functions will always return a value.

What are asynchronous functions?

Asynchronous functions is a function that returns the data back to the caller by a means of event handler (or callback functions ). The callback function can be called at any time (depending on how long it takes the asynchronous function to complete).

Why does Node JS use asynchronous non-Blocking IO?

You seem to be confusing asynchronous IO with asynchronous functions. node.js uses asynchronous non-blocking IO because non blocking IO is better. The best way to understand it is to go watch some videos by ryan dahl.

How to write promise-based code asynchronously in Node JS?

The asynchronous function can be written in Node.js using ‘async’ preceding the function name. The asynchronous function returns implicit Promise as a result. The async function helps to write promise-based code asynchronously via the event-loop.


1 Answers

Actually it sounds a little weird to create a "asynchronous" function without any further description or requirement. Usually, the usage is about avoiding long blocking periods. So if we have a function which triggers a database-query and we want to call another function when this is completed, that function should get called after the job is done and the original function instantely returns the control-flow back to Node (ECMAscript).

So if we have a construct like

function someJob( callback ) {
    // heavy work
    callback();
}

Now, this code still runs very synchronous. To bring that in an async state, we can invoke node's process.nextTick method

function someJob( callback ) {
    // heavy work
    process.nextTick( callback );
}

What happens now is, that Node will execute that callback method in a later run through its eventloop (and it does that somewhat intelligent too). Eventho nextTick claims to be much more efficient than setTimeout, its pretty much the same deal (from the ECMAscript coders perspective). So in a browser we could go like

function someJob( callback ) {
    // heavy work
    setTimeout( callback, 0 );
}

You would be correct if saying that the above example method doesn't make much sense at all, because the async state happens after the heavy work. Well, true. Infact, you would need to break down the heavy part into smaller peaces and make use of the same idea using .nextTick() to make this really efficient.

Thats also the reason for my confusion on the beginning, actually every task already offers callback possibilities for you.

like image 123
jAndy Avatar answered Nov 14 '22 23:11

jAndy