1

if you run the code snippet below you will notice that the param value is undefined. in the final function despite adding it. is there a way to add the parameter and have it show up in the success function. is there a way to add parameters to a callback function? edit.. to avoid some confusion the parameter is not available until sendPersonDetailsToServer

 function saveSuccess(param) {
     console.log('save success');
     console.log(param);
   }

   function sendPersonDetailsToServer(successCallback) {
     console.log('send person details to server');
     successCallback('myParameter');
   }
   
   function saveFiles(successCallback) {
                console.log('save Files');
                successCallback();
   }

   $(document).ready(function() {
     sendPersonDetailsToServer(function () {
        saveFiles(saveSuccess);
    });
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79

2 Answers2

3

You are not accepting the callback response parameter in sendPersonDetailsToServer callback method and also not passing it to saveSuccess method, thus getting undefined.

//Accept callback response parameter 
sendPersonDetailsToServer(function(response) {
  saveFiles(function() {
    //Pass it to saveSuccess
    saveSuccess(response);
  });
});

function saveSuccess(param) {
  console.log('save success');
  console.log(param);
}

function sendPersonDetailsToServer(successCallback) {
  console.log('send person details to server');
  successCallback('myParameter');
}

function saveFiles(successCallback) {
  console.log('save Files');
  successCallback();
}

$(document).ready(function() {
  //Accept callback response
  sendPersonDetailsToServer(function(response) {
    saveFiles(function() {
      //Pass it to saveSuccess
      saveSuccess(response);
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

The problem here is that your parm is not passed through the correct function. I've moved the param to the function saveFiles and it worked as expected

 function saveSuccess(param) {
     console.log('save success');
     console.log(param);
   }

   function sendPersonDetailsToServer(successCallback) {
     console.log('send person details to server');
     successCallback('myParameter');
   }
   
   function saveFiles(successCallback) {
                console.log('save Files');
                successCallback('myParameter'); // this is where your parameter should be passed
   }

   $(document).ready(function() {
     sendPersonDetailsToServer(function () {
        saveFiles(saveSuccess);
    });
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  • unfortunately as mentioned in the original question myparameter is not available in savefiles. it is only available in sendPersonDetailstoServer – Bryan Dellinger Aug 28 '18 at 12:57