我需要拒绝来自其主体之外的 promise ,以处理想要取消操作的用户的情况。
在这里,我需要通过在每个排队上传时调用 #start 来同时开始多个上传。
管理上传队列的类然后存储所有 promise 并使用 Ember.RSVP.all
当所有 promise 都已解决或一个已拒绝时处理。这工作正常。
现在,我想取消上传
App.Upload = Ember.Object.extend({
start: function() {
var self = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
self.startUpload() // Async upload with jQuery
.then(
function(resp) { resolve(resp) },
function(error) { reject(error) }
);
});
},
cancel: function() {
this.get('currentUpload').cancel() // Works, and cancels the upload
// Would like to reject the promise here
},
startUpload: function() {
return this.set('currentUpload', /* some jqXHR that i build to upload */)
}
});
我想了很多办法来处理,但是没有找到像
myPromise.reject(reason)
这样的方法。 .所以我所做的是存储
reject
Upload
中的函数实例并从我的 cancel
调用它方法,像这样:App.Upload = Ember.Object.extend({
start: function() {
var self = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
/* Store it here */
self.set('rejectUpload', reject);
/* ------------- */
self.startUpload() // Async upload with jQuery
.then(
function(resp) { resolve(resp) },
function(error) { reject(error) }
);
});
},
cancel: function() {
this.get('currentUpload').cancel() // Works, and cancels the upload
/* Reject the promise here */
var reject;
if (reject = this.get('rejectUpload')) reject();
/* ----------------------- */
},
startUpload: function() {
return this.set('currentUpload', /* some jqXHR that i build to upload */)
}
});
这对我来说听起来有点脏,我想知道是否有更好的方法来做到这一点。
谢谢你的时间 !
最佳答案
var deferred = Ember.RSVP.defer();
deferred.resolve("Success!");
deferred.reject("End of the world");
访问 promise (为 thening 等)
deferred.promise.then(function(){
console.log('all good');
},function(){
console.log('all bad');
});
关于ember.js - 从外部拒绝/解决 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313034/