S1r-Lanzelot
S1r-Lanzelot

Reputation: 2266

handling nested callback functions

I need some help structuring my function w/ callbacks. The question: Is my logic performing how I imagine it?

app = getApp(function(val1, val2){
    helperFunction(val3, function(val4, val5, callbackTwo){
       //...logic
       var val6 = ....
       callbackTwo(val6)
    },
    function(val6){
        //logic
    });
};

function helperFunction(val3, callback, callbackTwo){
    //logic
    callback(val4, val5, callbackTwo);
}

Basically it works. However depending on my first anonymous callback function's logic, is there any chance that my callbackTwo function will be triggered before the assignment of val6?

Upvotes: 1

Views: 85

Answers (1)

user229044
user229044

Reputation: 239220

That's pretty hilariously overwrought. Not sure why you're passing callback2 into/out of helperFunction, since it doesn't do anything with it. Instead, just invoke your 2nd callback's code directly in your original callback. Unless you can give us a reason (there is no obvious one), just put that logic inside your first callback.

I've cleaned it up as best I can, but you haven't given us real, syntactically-valid JavaScript to work with here:

App = getApp(function(val1, val2) {
  helperFunction(val3, function(val4, val5) {
    //...logic
    var val6 = ....
    //logic that uses val6, originally from callback2
  });
};

function helperFunction(val3, callback){
  //logic
  callback(val4, val5);
}

Note that "logic that uses val6" can just be another function invocation, but there is no reason to make that function an argument to helperFunction's callback.

Upvotes: 3

Related Questions