0

I have a function that does a backend call and fetches an array of names. The function is like this.

module.exports.getTxnList = function(index, callback) {
    ....some operations
    .....
    ....
    callback(null, response);
};

I have one more function where i want to call this function and get this list. Both the functions are located in the same file.

The other function is something like this.

module.exports.getTxnAvailability = function(index, lid, callback) {
    ...
    ...
    ...
};

I tried lot of things but i am not getting the data from the former function.

This is what i tried to do.

var that = this;
that.getTxnList(index, function(response){
    // Here you have access to your variable
    console.log("List: " + response);
})

And this

var txnList=this.getTxnList(index);

Any help will be appreciated.

Nikhil Tikoo
  • 365
  • 1
  • 9
  • 31

2 Answers2

1

Unfortunately you can't do this nodejs due to async behaviour:

var txnList = this.getTxnList(index);

Do like this:

//name the function for local use
var getTxnList = module.exports.getTxnList = function (index, callback) {
    ... }

module.exports.getTxnAvailability = function(index, lid, callback) {

    getTxnList(index, function(err, response){
        //here you have access to your variable
        //rest of your logic will be written here

        var txnList = response;
    }); 
};

Reference: to understand how async code works

Reference: to understand what is callback hell and how to solve it

Community
  • 1
  • 1
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
  • Not getting the data. – Nikhil Tikoo Dec 20 '16 at 06:51
  • I guess `this` is causing the problem, I've edited to remove `this` and added a name to your function. Also you're passing `null` as your first parameter and data in second so you need to pass 2 params in your callback function. – Shaharyar Dec 20 '16 at 06:55
  • this was needed. Otherwise it isn't able to recognize the function. But its working now. Thanks a ton for your help. You saved a lot of time :) – Nikhil Tikoo Dec 20 '16 at 07:01
  • Callbacks are always confusing, learn to use promises when you get a chance. – Shaharyar Dec 20 '16 at 07:04
  • Named function expressions don't create local variables. – Bergi Dec 20 '16 at 07:05
  • @Bergi But they create local functions – Shaharyar Dec 20 '16 at 07:06
  • [No they don't.](http://kangax.github.com/nfe/) The code you posted doesn't work. – Bergi Dec 20 '16 at 07:08
  • @Shaharyar I am getting the data inside the getTxnList. But when i try to access the txnList variable outside this function, it shows it as null. Any idea what's happening? – Nikhil Tikoo Dec 20 '16 at 07:10
  • @NikhilTikoo You can't access it outside the function, you'll have to write rest of your code inside the callback function. I am 2 references for you to understand async flow in the answer. – Shaharyar Dec 20 '16 at 07:15
  • @Shaharyar Oh ok. Understood. Thanks a lot :) – Nikhil Tikoo Dec 20 '16 at 07:19
  • @Bergi Thanks for the informative link, I've tested it's not working. I am amazed how it worked for OP. Admitted the fact you're always right in JS :) – Shaharyar Dec 20 '16 at 07:24
-1

You should be able to call the first function in your second function like

module.exports.getTxnAvailability = function(index, lid, callback) {
  module.exports.getTxnList(index, function(err, data) {
    console.log(data);
  });
};
lambert
  • 1
  • 2