CoffeeScript, como eu escreveria este exemplo de funções na fila, especialmente o loop?

StackOverflow https://stackoverflow.com/questions/4227367

  •  26-09-2019
  •  | 
  •  

Pergunta

Estou tentando obter alguns exemplos de como você faria algo diferente no CoffeeScript para JavaScript. Neste exemplo de funções de filas, estou confuso com a forma como você lidaria com isso no 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())();   
    }

Especialmente como eu reescreveria isso no CoffeeScript?

while (Array.length > 0) {
    (Array.shift())(); 
Foi útil?

Solução

f1 = (completeCallback) ->
  console.log('Waiting...')
  completeCallback()

funcs = [ f1, f2, f3 ]

next = ->
  if funcs.length > 0
    k = funcs.shift()
    k(next)

next()

Outras dicas

fun1 = -> alert 'Hello Fun1'
fun2 = -> alert 'Hello Fun2'

funqueue = [fun1, fun2]

el() for el in funqueue
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top