简体   繁体   English

为什么两次调用promise时RSVP Deferred都会产生错误

[英]Why RSVP Deferred produces error when promise is called twice

Why RSVP Deferred produces an error when the promise is called twice? 为什么当两次调用promise时,RSVP Deferred会产生错误?

It seems that there is a difference between deferred.promise.then().finally() and deferred.promise.then(); deferred.promise.finally() 看来deferred.promise.then().finally()deferred.promise.then(); deferred.promise.finally()之间有区别deferred.promise.then(); deferred.promise.finally() deferred.promise.then(); deferred.promise.finally() . deferred.promise.then(); deferred.promise.finally() Why? 为什么?

 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> 

EDIT: I found out how to fix the issue. 编辑:我发现了如何解决此问题。 See the Deferred3 in the code. 请参阅代码中的Deferred3。

I found that promise.then() (and other methods) returns a modified promise so you have to chain the then , finally , catch ... methods or you have to save the promise each time. 我发现promise.then() (和其他方法)返回了修改后的promise,因此您必须链接thenfinallycatch ...方法,否则每次都必须保存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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 `Promise.then` 在 React 组件中被调用两次,而不是在 console.log 中? - Why is `Promise.then` called twice in a React component but not the console.log? 未捕获(承诺中)错误:使用类型的操作调用时 - Uncaught (in promise) Error: When called with an action of type 承诺-在deferred.reject期间出现包装错误 - Promise - wrapping error during deferred.reject jQuery.when()Deferred和/或Promise数组的进度 - jQuery.when() progress for array of Deferred and/or Promise Ember.RSVP.Promise with sync - Ember.RSVP.Promise with sync 为什么attributeChangedCallback被调用两次? - Why is attributeChangedCallback called twice? 有没有办法从Jquery Deferred / Promise转到When-JS Promise - Is there a way to go from Jquery Deferred/Promise to When-JS Promise 当几个“ then”处理程序链接到一个延迟的处理程序时,为什么要调用多个“ fail”处理程序? - Why are multiple 'fail' handlers being called when several 'then' handlers are chained to a deferred? Node.Js函数在promise返回中调用一次时运行两次 - Node.Js function runs twice when called once on promise return 自定义 Promise 类的构造函数被调用两次(扩展标准 Promise) - Constructor of a custom promise class is called twice (extending standard Promise)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM