2

I am facing an issue in printing the two statements, I have two functions

var mongoose = require( 'mongoose' );
var_show_test = mongoose.model( 'test' );

exports.showTest = function(req,res) {
    var jsonString = [];

    var_show_test.find(function (err, testa) {
        console.log("In find");
    });

    console.log("In function");
}

but it is printing the statements in sequence

In function
In find

what i want is to print the statements in sequence e.g

In find
In function

I know it is happening due to asynchronous calling, I am little bit confused about callback functions. how to handle this in order to print the statements in sequence.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
JN_newbie
  • 5,492
  • 14
  • 59
  • 97
  • 1
    Callbacks and async programming in JS are hard to understand if you come from Java for example. Here there is a similar question about asynchronous javascript: http://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node – Salvatorelab Nov 21 '13 at 10:27

1 Answers1

2

For starter, if you simply want to log into console, then you can put the second console.log into your callback -

exports.showTest = function(req,res) {
    var jsonString = [];

    var_show_test.find(function (err, testa) {
        console.log("In find");
        console.log("In function");
    });
}

But, if you want to execute a function/method after your find callback executes, then you need to use a closure here -

exports.showTest = (function (callback) {
    return function(req,res) {
        var jsonString = [];

        var_show_test.find(function (err, testa) {
            console.log("In find");
            callback();
        });
    }
})(callbackFunc);

function callbackFunc() {
    console.log('In my callback!');
}
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • what will be in (callbackFunc)? – JN_newbie Nov 21 '13 at 05:44
  • @Java_NewBie: Passs the function/method name that you want to call inside the `find` callback. I am updating my answer to clarify. – MD Sayem Ahmed Nov 21 '13 at 05:45
  • When i run it, it prints. In my callback. then error on callback(), then In find. I want to print first "In find" and then "In my callback". I am going to check it again. – JN_newbie Nov 21 '13 at 05:52
  • @Java_NewBie: You are probably doing something wrong somewhere. It will be better if you can post the code snippet that you are using right now. – MD Sayem Ahmed Nov 21 '13 at 05:54
  • exports.showEmployeeTest = (function(callback){ return function(req,res) { var_show_test.find(function (err, employeedirectory, count) { console.log("In find"); callback(); }); } })(testCallBack()); function testCallBack(){ console.log("In test call back"); } – JN_newbie Nov 21 '13 at 05:57
  • @Java_NewBie: You should only pass `testCallBack`, not `testCallBack()` as argument. You only pass the function name, not call the function. – MD Sayem Ahmed Nov 21 '13 at 06:00
  • Hi Sayem, I have one more issue to discuss, can I post my code here? – JN_newbie Nov 21 '13 at 06:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41592/discussion-between-sayem-ahmed-and-java-newbie) – MD Sayem Ahmed Nov 21 '13 at 06:26