Coffeescript, how would I write this queued functions example, especially the looping?
-
26-09-2019 - |
Question
I'm trying to get some examples under my belt of how you would do something differently in CoffeeScript to JavaScript. In this example of queuing functions I'm confused at how you would do handle this in CoffeeScript
wrapFunction = (fn, context, params) ->
return ->
fn.apply(context, params)
sayStuff = (str) ->
alert(str)
fun1 = wrapFunction(sayStuff, this, ['Hello Fun1'])
fun2 = wrapFunction(sayStuff, this, ['Hello Fun2'])
funqueue = []
funqueue.push(fun1)
funqueue.push(fun2)
while (funqueue.length > 0) {
(funqueue.shift())();
}
Especially how would I rewrite this in CoffeeScript?
while (Array.length > 0) {
(Array.shift())();
Solution
f1 = (completeCallback) ->
console.log('Waiting...')
completeCallback()
funcs = [ f1, f2, f3 ]
next = ->
if funcs.length > 0
k = funcs.shift()
k(next)
next()
OTHER TIPS
fun1 = -> alert 'Hello Fun1'
fun2 = -> alert 'Hello Fun2'
funqueue = [fun1, fun2]
el() for el in funqueue
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow