방금 약속이 바뀌었고 잘못된 것이 있습니다. 그러나 무엇을 알 수는 없습니다. 다음 코드에서는 _getDevice()가 null을 반환하면 약속을 취소하고 싶습니다 (후드 아래 mongodb.findOneAsync()
).'then'에서 약속을 취소하십시오.
_getDevice가 null을 반환하면 저의 관점에서 약속을 취소해야하며 'CANCEL REPLY'로그가 표시되어야합니다. 하지만 대신 'SAVE REPLY'로그가 나타납니다.
나는 블루 버드 라이브러리를 사용하고 있습니다. 그 콜백의 실행이 약속이 해결되었음을 의미로
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);
});
대단한 답변입니다. 고맙습니다 ! – doobdargent