Question

I need to override some behaviour of function, that goes after it calls other function. The problem is that this parent function is a library and I dont want to change it, so solutions like make some flag or another change of this function is not so good. I know that I got an caller object in function that I can change, so maybe I can figure out smth with it. Heres the example:

function parent()
{
  console.log("some need stuff");
  some.handler.function.from.config.that.i.can.change();
  console.log("need omit this right till the end");
}

function child()
{
  console.log("need to somehow stop evaluation of " + child.caller + " function");
}

As a ruby programmer I know there is lambdas with which you can terminate evaluation from inner scope of closure. But Im not sure how to do this from javascript.

Was it helpful?

Solution

You can't do that directly. (Moreover .caller is obsolete)

You can however use a dirty trick:

try{
    parentFunction();//calls child
}catch(e){
   //done
}

function child(){
    doWhatever();
    throw new Error("this will hopefully propagate");
}

Fiddle

This will only work assuming the parent does not catch exceptions itself when calling the child.

Moreover, it is generally a bad idea to use exceptions for flow control. Use this as a last resort.

OTHER TIPS

Call a new function that you do control before you call the library, and wrap the call to the library you cannot modify in a try/catch block.

For example:

function meta-parent()
{
  try {
    parent();
  }
  catch (e){
      // don't really be empty!
  }
}

function parent()
{
  console.log("some need stuff");
  some.handler.function.from.config.that.i.can.change();
  // Nothing below here will run because of your child's exception
  console.log("need omit this right till the end");  
}

function child()
{
  console.log("need to somehow stop evaluation of " + child.caller + " function");
  throw new Error(); //breakout!
}

Note inspiration: Breaking a parent function from within a child function (PHP Preferrably)

As Benjamin Gruenbaum points out, the use of exceptions for flow control should be regarded as a last resort, however does the Promises/A proposal (in some implementations) not legitimize such exploitation of exceptions in this way?

I do take on board that an (otherwise uncaught) error thrown from a child function is maybe not what the Promises/A authors had in mind (ie we're still arguably talking about a "dirty trick"), however the Promises/A proposal doesn't specifically exclude such error propagation either.

The Q lib would be ideal for this problem, allowing something like this :

Q.fcall(parentFunction).then(function (value) {
    alert(value);
}, function (error) {
    alert(error);
});

fiddle

Please note that, for convenience, I also use jQuery in the fiddle but only to give functionality to the buttons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top