CoffeeScript, ¿cómo iba a escribir este ejemplo funciones en cola, especialmente el bucle?
-
26-09-2019 - |
Pregunta
Estoy tratando de conseguir algunos ejemplos en mi haber de cómo le gustaría hacer algo diferente en CoffeeScript tener JavaScript. En este ejemplo de funciones que hacen cola estoy confundido de lo que haría en este mango 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())();
}
Sobre todo ¿cómo iba a volver a escribir esto en CoffeeScript?
while (Array.length > 0) {
(Array.shift())();
Solución
f1 = (completeCallback) ->
console.log('Waiting...')
completeCallback()
funcs = [ f1, f2, f3 ]
next = ->
if funcs.length > 0
k = funcs.shift()
k(next)
next()
Otros consejos
fun1 = -> alert 'Hello Fun1'
fun2 = -> alert 'Hello Fun2'
funqueue = [fun1, fun2]
el() for el in funqueue
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow