0

I have a function like so:

myFunction(callback){
    somePromise
    .then(function (returnValue) {
        //figure out how to do a multiple arguments here to callback
        callback(returnValue); //figure out how to pass the arguments that were passed to myFunction in here in addition to the returnValue of somePromise.
    });
}

I want to be able to call it like this:

myFunction(myCallBack, callbackArg1, callbackArg2);
myFunction(differentCallback, callbackArg1, callbackArg2,callbackArg3, callbackArg4);
myFunction(thirdCallback);

I want to be able to allow users to pass in a callback function to myFunction, and execute using the callback using the return value of the Promise, AND any other args passed in to myFunction()

I tried to use callback.apply(), but I can't seem to get the arguments passed to it, since I am out of scope in the Promise then() block.

Derek
  • 11,427
  • 30
  • 120
  • 224

1 Answers1

0

You can do it:

myFunction(callback, arg1, arg2){
    somePromise
    .then(function (returnValue) {
        //figure out how to do a multiple arguments here to callback
        callback(returnValue, arg1, arg2);
    });
}

myFunction(myCallBack, callbackArg1, callbackArg2);
Faly
  • 12,802
  • 1
  • 18
  • 35