Were `do…while` loops left out of CoffeeScript…?

前端 未结 5 527
栀梦
栀梦 2021-02-03 17:13

In CoffeeScript, the while loop comes standard:

while x()
   y()

However, the following1 doesn\'t work:



        
5条回答
  •  无人共我
    2021-02-03 17:45

    Your guess is correct: There is no do-while equivalent in CoffeeScript. So you'd typically write

    y()
    y() while x()
    

    If you find yourself doing this often, you might define a helper function:

    doWhile = (func, condition) ->
      func()
      func() while condition()
    

提交回复
热议问题