Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I have function, f1, and I want to fire a call back when f1 is complete, how to do this?

How can I create a function, that will call another function, and when it completes, fire another callback function?

so existing functions are:

function f1(..) {..}

function myCallback() {...}

Now is it possible to make f1 fire and finish, THEN run myCallback()?

like image 364
Blankman Avatar asked Dec 09 '25 22:12

Blankman


2 Answers

Provide a function reference as a parameter to the function you're calling.

function f1(fn) {

  // ...

  if (typeof fn === 'function') {    
    fn();
  }
}

// can be a defined function name or a variable holding a reference to a function
f1(myCallback);
like image 184
lincolnk Avatar answered Dec 12 '25 12:12

lincolnk


f1();
myCallback();

… unless f1 is asynchronous, in which case f1 would have to be edited to accept a callback and run it when it is finished. Since there are multiple things that could make a function asynchronous, it isn't possible to give a simple "…and this is how" answer without a lot more detail.

like image 37
Quentin Avatar answered Dec 12 '25 10:12

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!