• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Though currently I am prefer to use CofferScript than Swift, I have thought that the defer of Swift2 is pretty cool. So I wrote a snippet that accomplishes the feature by using yield 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 Png genki on Sat 10 Oct 2015 at 00:36

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry2390
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ