【发布时间】:2015-10-21 23:12:17
【问题描述】:
我有这样的 CoffeeScripted 类:
class Hair
truncate: () ->
DoAsyncStuffWithMyDB ->
console.log "Async Stuff With My DB completed"
现在我想使用这个类,例如:
doghair = new Hair
doghair
.truncate()
.then(() ->
#do stuff
)
我如何使用 Bluebird 做到这一点?
【问题讨论】:
-
DoAsyncStuffWithMyDB 是否已经返回了一个承诺?如果不是,您需要 [promisify](stackoverflow.com/questions/22519784/how-do-i-convert-an-existing-callback-api-to-promises) 它
-
DoAynscStuffWithMyDB()接受哪些参数?要承诺某事,需要知道它需要什么论据。如果它遵循 node.js 异步调用约定,那么您可以使用 Bluebird 的promisify()函数。如果没有,您必须制作自定义包装器以返回正确解析的承诺。