如何从称为函数的子进程中退出父调用函数而不抛出错误

时间:2017-07-27 17:37:51

标签: javascript function

const funB = (param1) => {

  if(param1 === "test") {
    return "I am in testing mode"; // exit from this function
  }

  const value = "I am running the production flow";

  // Do other stuff

}

//////////refactor funB for usabilty purpose ////////////

const funA = (parm1)=> {

  if(param1 === "test") {
    return "I am in testing mode";
  }

  return "I am running the production flow";
}


const funB = (param1) => {

  // Call funA
  const value = funA();

  // Do other stuff

}

问题:重构后,流程一直在执行,因为在funA()

时,funB()的调用没有打破param1==="test"执行流程

问:如何在重构代码中解决这个问题?

1 个答案:

答案 0 :(得分:0)

在我看来,您没有为function funA参数>设置任何默认值在parm1内调用funA时,function funB也未向const value = funA();传递任何值:
const funA = (parm1)=> { if(param1 === "test") { return "I am in testing mode"; } return "I am running the production flow"; } const funB = (param1) => { const reValue = funA(param1); if(reValue == "I am in testing mode") { return; } // Do other stuff }

您可以使用必要的参数调用funA()并在funB中进行必要的检查:

<div id = 'maincontainer'>
    <div id="fix-width-container">
        <div id = 'left'></div>
        <div id = 'middle'></div>
        <div id = 'right'></div>
    </div>
</div>

<style>
    #fix-width-container {
        width: 1206px;
        height: 520px;

    }

    #maincontainer {
        overflow-y: hidden;
        overflow-x: scroll;


    }

    #left {
        width: 400px;
        height: 500px;
        border-style: solid;
        border-width: 1px;
        border-color: black;
        float: left;

    }

    #middle {
        width: 400px;
        height: 500px;
        border-style: solid;
        border-width: 1px;
        border-color: black;
        float: left;


    }

    #right {
        width: 400px;
        height: 500px;
        border-style: solid;
        border-width: 1px;
        border-color: black;
        float: left;

    }
</style>