Were `do...while` loops left out of CoffeeScript...?
In CoffeeScript, the while
loop comes standard:
while x()
y()
However, the following1 doesn't work:
do
y()
while x()
And this is simply sugar for the first example:
y() while x()
Does CoffeeScript come with a built-in loop that executes at least once?
1As an aside, do
is a keyword — it's used to call anonymous functions.
Solution 1:
The CoffeeScript documentation says:
The only low-level loop that CoffeeScript provides is the while loop.
I don't know of a built-in loop that executes at least once, so I guess the alternative is
loop
y()
break if x()
Solution 2:
I know that this answer is very old, but since I entered here via Google, I thought someone else might as well.
To construct a do...while loop equivalent in CoffeeScript I think that this syntax emulates it the best and easiest and is very readable:
while true
# actions here
break unless # conditions here
Solution 3:
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()