handloomweaver
handloomweaver

Reputation: 5011

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

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())(); 

Upvotes: 3

Views: 1333

Answers (2)

yfeldblum
yfeldblum

Reputation: 65435

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

funcs = [ f1, f2, f3 ]

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

next()

Upvotes: 3

yfeldblum
yfeldblum

Reputation: 65435

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

funqueue = [fun1, fun2]

el() for el in funqueue

Upvotes: 4

Related Questions