1

My requirement is very simple.

 subscribePresence();
 getValidationData();

getValidationData() must be called only when subscribePresence(); finishes its execution. is there any way of doing it ?

monda
  • 3,809
  • 15
  • 60
  • 84
  • What does the first function execute? – kapantzak May 05 '16 at 05:02
  • It seems like you know what a *callback* is, so what have you tried? – vol7ron May 05 '16 at 05:09
  • 1
    That's exactly how Javascript works. The code you have supplied will guarantee that `subscribePresence();` finishes executing before `getValidationData();` is called. JavaScript executes code in order. Whether or not `subscribePresence();` starts some other asynchronous task has nothing to do with it finishing its execution. – Paul May 05 '16 at 05:09
  • Possible duplicate of [Create a custom callback in JavaScript](http://stackoverflow.com/questions/2190850/create-a-custom-callback-in-javascript) – Matt Komarnicki May 05 '16 at 05:14
  • 1
    Show us the code for `subscribePresence()` so we can see whether it is asynchronous or not and, if so, how to know when it is done with its work. – jfriend00 May 05 '16 at 05:18

1 Answers1

1

Pass getValidationData(); as callback function into first function.

Ex:

function subscribePresence(callback){
  //do your stuffs
  callback();
}

Now call your subscribePresence() as below

subscribePresence(getValidationData);
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200