标签:
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)
标签:
原文地址:http://www.cnblogs.com/buhaiqing/p/5385435.html