码迷,mamicode.com
首页 > 其他好文 > 详细

[CoffeeScript]使用Yield功能

时间:2016-04-13 07:19:03      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

CoffeeScript 1.9 开始提供了类似ES6的yield关键字。 自己结合co和bluebird做了个试验。

co -- http://npmjs.org/package/co  -- for generator

bluebird -- https://www.npmjs.com/package/bluebird  for Promise

co = require ‘co‘
Promise = require ‘bluebird‘

msg = "good"
func1 = () ->
  new Promise((resolve)->
    setTimeout(
      ()->
        console.log "func1"
        resolve({a:10,b:2})
    , 1000))


func2 = (opts) ->
  {a,b}= opts
  new Promise((resolve, reject)->
    setTimeout(()->
      console.log "func2", a, b
      console.log msg
      resolve(a * b * 2)
    , 1000)
  )

func3 = (r)->
  new Promise((resolve)->
    console.log "the result is #{r}"
    resolve()
  )
func4 = ()->
  new Promise (resolve)->
    console.log "done"
    resolve()

calc1= (r) ->
  yield func3(r)
  yield func4()

calc = ()->
  opts = yield func1()
  r = yield func2(opts)
  yield calc1(r)
#  yield func3(r)
#  yield func4()


co(calc) 

 

[CoffeeScript]使用Yield功能

标签:

原文地址:http://www.cnblogs.com/buhaiqing/p/5385435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!