最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

closures - how to terminate parent function execution in javascript? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Jun 29, 2013 at 19:15 sandricsandric 2,4703 gold badges24 silver badges29 bronze badges 2
  • I've posted a possible solution to your problem, however - I think that it's as good as it gets with your requirements. However, doing what I suggested usually is not the best approach. I suspect the XY problem. If there is a more specific issue you're facing here, please consider opening a new question with the actual problem. Using exceptions for this is strongly discouraged. – Benjamin Gruenbaum Commented Jun 29, 2013 at 19:26
  • you can always throw, but async techniques are preferred when possible. – dandavis Commented Jun 29, 2013 at 21:19
Add a ment  | 

3 Answers 3

Reset to default 8

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.

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.

发布评论

评论列表(0)

  1. 暂无评论