1

I'm newbie in NodeJs. This is my code for learning asynchronous function.

//--------------------- MAIN ---------------
console.log("Endpoint 1\r\n");

testThread(1000000000,function (result){
    console.log(">>>>"+result+"\r\n");
});

console.log("Endpoint 2\r\n");

//------------------------------------------


function testThread(data,callback) {    
    //take a long time
    for(j=0;j<data;j++) {
        a = 122342342342424242431*3543652636364;
    }
    //          
    callback(a);
}

Run it:

node testthread.js

Always the result is:

Endpoint 1

>>>>4.335387639806787e+32

Endpoint 2

System prints "Endpoint 1", take 2 seconds, it prints ">>>>4.335387639806787e+32" after then it prints "Endpoint 2"

I'm not found the asynchronous here. It should be:

Endpoint 1

Endpoint 2

>>>>4.335387639806787e+32

Please explain me.

2 Answers 2

1

Asynchronous functions are functions that call other functions that are asynchronous. There is not other way to implement asynchronous functions in javascript/node.js. Which at first looks like a chicken and egg problem doesn't it? How can one write an asynchronous function if the requirement is that it must call another asynchronous function in order to be asynchronous?

The answer is that the lowest level asynchronous function must be implemented in C.

Fortunately, javascript/node.js has several built-in asynchronous functions implemented in C that we can use as building blocks in javascript to build our own asynchronous functions. Examples of such functions include the http.request() method, the socket.listen() method and probably the simplest setTimeout() and setInterval().

Here's an example of how one could rewrite your code to achieve asynchronous processing;

function long_task (repeat_number, callback) {
    // setTimeout is asynchronous, so keep calling it until we're done

    function loop () {
        if (repeat_number > 0) {
            repeat_number --;
            setTimeout(loop,1);
        }
        else {
            callback("I'm done!");
        }
    }

    loop();
}

console.log("calling long task");
long_task(10000,function(x){console.log(x)});
console.log("long task started");
Sign up to request clarification or add additional context in comments.

1 Comment

@pfried's answer is even better because it's not only asynchronous but also parallel (if you have multiple cores).
1

This is a total synchronous code and as node.js is single threaded it wont start a second thread this way.

There are several asynchronous tasks like network requests or database calls, but this one is not.

You would have to spawn a child process to have this asynchronous.

See http://nodejs.org/api/child_process.html for more information

1 Comment

Thank all so much, my confusion, I got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.