Though currently I am prefer to use CofferScridefer
of Swift2 is pretty cool. So I wrote a snippet that accomplishyield
as like the manner of co
is doing.
1 defer = (g) -> 2 deferred = [] 3 go = (i) -> 4 {value,done} = do i.next 5 unless done 6 deferred.push value 7 go i 8 exit = -> 9 do f for f in deferred 10 process.removeListener 'exit', exit 11 process.on 'exit', exit 12 try go do g 13 catch then do exit 14 15 foo = (type) -> defer -> 16 console.log "#{type} 1" 17 yield -> console.log "#{type} deferred 1!" 18 yield -> console.log "#{type} deferred 2!" 19 console.log "#{type} 2" 20 if type == "exit" then do process.exit 21 else throw "throw" 22 console.log "3" 23 24 foo "throw" 25 foo "exit" 26 27 # RESULTS 28 # 29 # throw 1 30 # throw 2 31 # throw deferred 1! 32 # throw deferred 2! 33 # exit 1 34 # exit 2 35 # exit deferred 1! 36 # exit deferred 2!
posted by
genki
on Sat 10 Oct 2015
at 00:36