【发布时间】:2015-01-23 19:23:04
【问题描述】:
我刚刚切换到 Promise,我确定我做错了什么,但我不知道是什么。
在下面的代码中,如果 _getDevice() 返回 null (它是 mongodb.findOneAsync() 在引擎盖下),我想取消承诺。
从我的角度来看,如果 _getDevice 返回 null,则应取消承诺,我应该会看到“CANCEL REPLY”日志。但相反,我得到了“保存回复”日志。
我正在使用 bluebird 库。
var promise = Promise.props({
device: _getDevice(event.deviceId),
events: _getEvents(event.deviceId, event.date)
}).cancellable();
promise.then(function (result) {
if (! result.device) {
return promise.cancel()
}
var device = new Device(result.device);
var events = result.events;
// ...
return db.collection(collections.devices).saveAsync(device.toMongoDocument());
}).then(function () {
console.log('SAVE REPLY');
return req.reply(null);
}).catch(Promise.CancellationError, function (err) {
console.log('CANCEL REPLY')
return req.reply(null);
}).catch(function (error) {
return req.reply(error);
});
【问题讨论】:
标签: promise bluebird cancellation