1

My code contains the function calls as follows

module.exports = {


 func1() {

   // var parentThisScope = this;
   this.func2(param, function(err, data) {

     this.func4(data, function(err, result) {

      return result; // problem
     });
   });
 },
 func2() {
  // some IO
 },
 func3() {
  // some IO
 },
 func4() {
   this.func3(params, function(err, data){
     // some IO // problem
   });
 }
}

But, when I try to access the functions func4() from inside of func2() and func3() from inside of func4(), it throws error this.func4() and this.func3() is not a function. I tried adding a var to store the root scope(as shown in the code I stored this in the variable parentThisScope). It seems like when I call a property the scope of this changes as well. If that is the case, can anyone explain what exactly is happening and how to deal with it in a better way instead of just storing the rootscope.

Parth Vyas
  • 467
  • 4
  • 16
  • 1
    You really need to provide a [mcve]. The value of this depends on how a function is called, and it really isn't clear how you are calling most of those functions. – Quentin Jan 13 '17 at 19:27

0 Answers0