Coffeescript, how would I write this queued functions example, especially the looping?

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

  •  26-09-2019
  •  | 
  •  

문제

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())(); 
도움이 되었습니까?

해결책

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

funcs = [ f1, f2, f3 ]

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

next()

다른 팁

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

funqueue = [fun1, fun2]

el() for el in funqueue
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top