Stopping a JavaScript function when a certain condition is met

前端 未结 6 1928
无人共我
无人共我 2020-12-23 19:04

I can\'t find a recommended way to stop a function part way when a given condition is met. Should I use something like exit or break?

I am

6条回答
  •  时光取名叫无心
    2020-12-23 19:31

    Use a try...catch statement in your main function and whenever you want to stop the function just use:

    throw new Error("Stopping the function!");
    

提交回复
热议问题