承诺 CoffeeScript Class

Promisfy CoffeeScript Class

我有这样的 CoffeeScripted class:

class Hair
  truncate: () ->
    DoAsyncStuffWithMyDB ->
      console.log "Async Stuff With My DB completed"

现在我想用这个class,例如:

doghair = new Hair
doghair
.truncate()
.then(() ->
    #do stuff
)

我如何使用Bluebird做这样的事情?

自定义方式是这样实现的:

Promise  = require('bluebird')
class Hair
  truncate: () ->
    new Promise((resolve, reject) ->
      DoAsyncStuffWithMyDB.create data, (err,res) ->
        if err
          reject err
        else
          resolve true
    )

现在您的实例化对象可以 then 了。恭喜!