Function Calling Execution in nodejs

Hitu Bansal

I have this code.

for(var i = 0; i< bookmarks.length; i++){
        (function(i) {
            parseArticle(i,bookmark_id,bookmark_url,function(err,result){
                console.log("Success");
            });

        })(i);

}   


function parseArticle(i,bookmark_id,bookmark_url,callback) {
            diffbot.article({uri: url}, function(err, response) {
                console.log("Diffbot is" );
            });
            console.log("Parse Article" );
    callback(null,i);
};

Now output is coming like this

Parse Article

Sucess

Diffbot is

I want to execute function in this way so output will be like

Diffbot

Parse Article

Sucess

Can anybody tell me what is exact problem here and how to resolve that

Thanks

jfriend00

You need to learn what an asynchronous function and its completion callback are. When you understand that, you will understand why "Parse Article" is printed first and will have a better idea how you should structure your code. In a nutshell, an asynchronous functions starts an operation (which usually involves timers or networking or I/O of some kind) and the rest of your code continues to execute. Then, some time LATER, the asynchronous operation completes and the completion callback is called.

The key to using an asynchronous operation is that ALL activity that you want to happen after the asynchronous operation and all activity that you want to use the result of the asynchronous activity MUST be inside the callback that indicates the completion of the async activity.

In your particular case, you can achieve the desired output by putting things into the diffbot.article callback. This should generate this log:

Diffbot
Parse Article
Sucess

function parseArticle(i,bookmark_id,bookmark_url,callback) {
      diffbot.article({uri: url}, function(err, response) {
            // put everything in here that should occur after the async
            // operation is done
            console.log("Diffbot is" );
            console.log("Parse Article" );
            callback(null,i);
      });
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling function with callback in nodejs

From Dev

Calling a function in a callback function in NodeJs

From Dev

How to wait for a function execution in NodeJS

From Dev

NodeJS calling readline within a function

From Dev

Calling nodeJS from angularJS function

From Dev

Calling a function on the result of a promise in NodeJS

From Dev

Question 1: execution time of calling the function

From Dev

Non-execution of statement in function calling

From Dev

Add global function to every execution of NodeJS

From Dev

Function execution context, browser vs. nodejs

From Dev

firebase Nodejs async cloud function not finishing execution

From Dev

How to measure the execution time of an asynchronous function in nodejs?

From Dev

Is there a way to list all nodejs function execution time?

From Dev

Return html response to calling function in nodejs

From Dev

NodeJS module function convention - calling own functions

From Dev

Nodejs function call issue in calling API

From Dev

NodeJs Javascript Calling a Function Dynamically From an Array

From Dev

Mocha Test for NodeJS Function not calling Callback

From Dev

Calling a module function inside a Nodejs callback

From Dev

NodeJS MSSQL driver Passing data to a calling function

From Dev

calling websocket send function from within .then() in nodejs

From Dev

Calling C function from returned pointer in NodeJS

From Dev

Is there a way to call a function before calling the controllers in nodejs

From Dev

How can calling a function pointer saved from a previous execution fail?

From Javascript

order of code execution in async await not working properly in a function in nodejs?

From Dev

Calling Function from Nodejs at Client side: require is not defined

From Dev

NodeJS Calling a function when a txt. file is updated

From Dev

nodejs Q.all promises on function calling itself

From Dev

Nodejs - Re-Calling function on error callback - Is there a non blocking way?

Related Related

  1. 1

    Calling function with callback in nodejs

  2. 2

    Calling a function in a callback function in NodeJs

  3. 3

    How to wait for a function execution in NodeJS

  4. 4

    NodeJS calling readline within a function

  5. 5

    Calling nodeJS from angularJS function

  6. 6

    Calling a function on the result of a promise in NodeJS

  7. 7

    Question 1: execution time of calling the function

  8. 8

    Non-execution of statement in function calling

  9. 9

    Add global function to every execution of NodeJS

  10. 10

    Function execution context, browser vs. nodejs

  11. 11

    firebase Nodejs async cloud function not finishing execution

  12. 12

    How to measure the execution time of an asynchronous function in nodejs?

  13. 13

    Is there a way to list all nodejs function execution time?

  14. 14

    Return html response to calling function in nodejs

  15. 15

    NodeJS module function convention - calling own functions

  16. 16

    Nodejs function call issue in calling API

  17. 17

    NodeJs Javascript Calling a Function Dynamically From an Array

  18. 18

    Mocha Test for NodeJS Function not calling Callback

  19. 19

    Calling a module function inside a Nodejs callback

  20. 20

    NodeJS MSSQL driver Passing data to a calling function

  21. 21

    calling websocket send function from within .then() in nodejs

  22. 22

    Calling C function from returned pointer in NodeJS

  23. 23

    Is there a way to call a function before calling the controllers in nodejs

  24. 24

    How can calling a function pointer saved from a previous execution fail?

  25. 25

    order of code execution in async await not working properly in a function in nodejs?

  26. 26

    Calling Function from Nodejs at Client side: require is not defined

  27. 27

    NodeJS Calling a function when a txt. file is updated

  28. 28

    nodejs Q.all promises on function calling itself

  29. 29

    Nodejs - Re-Calling function on error callback - Is there a non blocking way?

HotTag

Archive