在调用我的承诺函数之前,我有几个非常基本的设置步骤,我正在考虑将它们包装在 try/catch 块中,因为这似乎是最简单的方法。但是,对我来说似乎有点脏。
我是否应该创建一个返回 Promise 的函数,即使它非常简单?这是一个例子。
try
thingyId = req.params.id # here I am 99.999% sure that params is defined,
# but if for some bizarre reason it's not, I'd like to handle that error
# instead of breaking the whole program
catch
console.log "error: " + e
# do normal promisified functions
或者我应该把它写成
setThingyId = (req) ->
return new Promise (resolve, reject) !->
if req.hasOwnProperty "params"
resolve req.params.id
else
reject new Error "no params"
setThingyId(req)
.then (deviceId) ->
# other promisified functions