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) 

 

相关文章:

  • 2021-10-04
  • 2022-02-26
  • 2022-12-23
  • 2021-06-18
  • 2021-07-11
  • 2021-08-12
  • 2022-12-23
猜你喜欢
  • 2021-05-20
  • 2021-12-09
  • 2021-10-06
  • 2021-10-22
  • 2022-12-23
  • 2022-01-24
  • 2022-12-23
相关资源
相似解决方案