促进CoffeeScript类

时间:2015-10-02 15:02:12

标签: node.js promise bluebird

我有这样的CoffeeScripted类:

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

现在我想使用这个类,例如:

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

我如何使用Bluebird做到这一点?

1 个答案:

答案 0 :(得分:0)

这是如何通过自定义方式实现的:

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

现在您的实例化对象是then - 能够。恭喜!