How to get a variable returned across multiple functions - Javascript/jQuery

16,420

Solution 1

Change your callback (at the call site) such that you capture the return value of functionD. Then, change functionD so that it returns array2. I've added this access to the example below as a convenience. (Also, be sure to include semicolons where "required" if you want to make JSLint happy.)

classOne = {
  initFunctionA : function() {
    var self = this;

    classTwo.functionB(function() {
        var array2 = functionD.apply(self, arguments);

        // ACCESS ARRAY2 HERE
    }, array);
  },
  functionD : function(data, array) {
    var array2 = // modifications to array

    return array2;
  }
};

{...}

classTwo = {
  functionB : function(callback, array) {
    $.ajax({
      success: function(ret){
        classTwo.functionC(ret, callback, array)
      }
    });
  },
  functionC : function(ret, callback, array) {
     callback(ret.data.x, array);
  }
};

Solution 2

You can't make it work with a pattern like you've written there; it's simply not possible in Javascript because there's no such thing as "waiting". Your ajax code has to take a callback parameter (which you've got, though it's not clear where it comes from or what it does), and that initial function should pass in code to do what you need with the array after the ajax call finishes.

Solution 3

I would use an object constructor:

function ClassOne() {
    this.array2 = [];
}

ClassOne.prototype.initFunctionA = function() {
    // ...
}

ClassOne.prototype.functionD = function(data, array) {
    // Can use array2 EX: this.array2
}

var classOne = new ClassOne();
Share:
16,420
Kyle Cureau
Author by

Kyle Cureau

Currently building crone.ai and hakeema. Reach me on Twitter

Updated on June 05, 2022

Comments

  • Kyle Cureau
    Kyle Cureau almost 2 years

    This question in summary is to figure out how to pass variables between javascript functions without: returning variables, passing parameters between primary functions, using global variables, and forcing function 1 to wait for function 2 to finish. I figured out a jQuery solution and posted in below (in the answers section).


    Old Post: I initialize a set of four functions, each calling on each other in a different way. At the end of it, I need the final modified product (an array) returned to the initializing function.

    Global variables don't force the initial function to wait. And returning it backwards four times doesn't work either. How do you pass a modified variable back to its initializing function, if you can't return it? Or why isn't it returning?

    (the maze starts at initFunctionA, ends at functionD)

    classOne = {
      initFunctionA : function() {
        classTwo.functionB(functionD, array);
        // I NEED ACCESS TO ARRAY2 HERE
      },
      functionD : function(data, array) {
        var array2 = // modifications to array
      }
    }
    
    {...}
    
    classTwo = {
      functionB : function(callback, array) {
        $.ajax({
          success: function(ret){
            classTwo.functionC(ret, callback, array)
          }
        });
      },
      functionC : function(ret, callback, array) {
         callback(ret.data.x, array);
      }
    }