为什么RSVP Deferred会在两次调用promise时产生错误?
似乎deferred.promise.then().finally()
和deferred.promise.then(); deferred.promise.finally()
之间存在差异。为什么呢?
RSVP.on('error', function(reason) {
console.log('Error: ' + reason);
});
var deferred = RSVP.defer();
var deferred2 = RSVP.defer();
var deferred3 = RSVP.defer();
var promise3 = deferred3.promise;
deferred.promise.then(function() {
console.log('Resolved');
}, function() {
console.log('Rejected');
}).finally(function() {
console.log('Finally');
});
deferred2.promise.then(function() {
console.log('Resolved2');
}, function() {
console.log('Rejected2');
});
deferred2.promise.finally(function() {
console.log('Finally2');
});
promise3 = promise3.then(function() {
console.log('Resolved3');
}, function() {
console.log('Rejected');
});
promise3.finally(function() {
console.log('Finally3');
});
deferred.reject('Reject!');
deferred2.reject('Reject2!');
deferred3.reject('Reject3!');
<script src="https://cdnjs.cloudflare.com/ajax/libs/rsvp/4.8.1/rsvp.js"></script>
编辑:我发现了如何解决这个问题。请参阅代码中的Deferred3。
答案 0 :(得分:0)
我发现promise.then()
(和其他方法)会返回修改后的承诺,因此您必须将then
,finally
,catch
...方法链接起来每次都保存promise
。
RSVP.on('error', function(reason) {
console.log('Error: ' + reason);
});
var deferred = RSVP.defer();
var promise = deferred.promise;
promise = promise.then(function() {
console.log('Resolved');
}, function() {
console.log('Rejected');
});
promise.finally(function() {
console.log('Finally');
});
deferred.reject('Reject!');
<script src="https://cdnjs.cloudflare.com/ajax/libs/rsvp/4.8.1/rsvp.js"></script>