Coffescript ، كيف يمكنني كتابة مثال وظائف قائمة الانتظار ، وخاصة الحلقات؟

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

  •  26-09-2019
  •  | 
  •  

سؤال

أحاول الحصول على بعض الأمثلة تحت حزامي عن كيفية القيام بشيء مختلف في Coffescript إلى JavaScript. في هذا المثال من وظائف قائمة الانتظار ، أنا في حيرة

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

خاصة كيف يمكنني إعادة كتابة هذا في Coffescript؟

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