0

How to call function inside function in ionic3 i have tried with this function but not working and i got error in console '=>' expected tell me anyone how to fix this?

shareProductWithSelectedUser(event) {
  var self = this;
  var  arr1 = "";
  var  arr2 = "";
  var  arr3 = "";

  // Iterating loop - continue only after callback from Firerbase.
    var x = 0;
    var loopArray(arr1, arr2, arr3) {

    // here calling inner function for share product with three param(userId, userTypeId, callback)
    self.callFuncForShareProduct(arr1[x], arr2[x], arr3[x], (function) =>  {
       // any more items in array? continue loop
        if(x < arr1.length) {
          this.loopArray(arr1, arr2, arr3);
         };
      });
      // start 'loop'
      loopArray(self.userIdListForShareProduct, self.userTypeIdListForShareProduct, self.userObjectListForShare);
    }

2
  • Very confusing code. Is loopArray a recursive function? And what is shareProductWithSelectedUser? Commented May 23, 2018 at 6:16
  • i have call 3 function one by one? Commented May 23, 2018 at 7:38

1 Answer 1

3

So first let's simplify your code

executeFunction(event) {
  var self = this;

  var innerFunction(args) {

      self.doSomething('somevariable', (function) =>  {
        if(/* expression */) {
          this.innerFunction(args);
         };
      });

      doAnotherFunction();
    }
}

So, you create a function, with an inner function and want the innerfunction to call itself and also call another function outside of the scope.

Now. The problems.

  • function is a reserved keyword and can't be used like that in a lambda.
  • creating an inner function doesn't work like that. Use function innerFunction(args) or var innerFunction = (args) => {}

You can rewrite your code with both of the methods in the second bullet point. I'll show you an example for the lambda.

executeFunction(event) {
  var innerFunction = (args) => {

      // the lambda allows you to keep the original scope, so you can still use `this`
      this.doSomething('somevariable', () =>  {
        if(/* expression */) {

           // innerFunction isn't defined globally but scope is maintained so you can call it like so
          innerFunction(args);
         };
      });

      this.doAnotherFunction();
    }
}

So after doing a double take on your code I'm not sure if you're creating an inner function but the code above should solve your other issues as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Please do note that calling executeFunction only creates the inner function but doesn't execute it. If you want to execute, add innerFunction(someArgs); before the closing tag of executeFunction
thank you sir for this information regarding to the inner function and i try this code and its woking for me

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.