Ember.js 拒绝/解决外部承诺

Ember.js 拒绝/解决外部承诺,ember.js,promise,cancellation,Ember.js,Promise,Cancellation,我需要拒绝来自外部的承诺,以处理想要取消操作的用户的情况 在这里,我需要同时启动几个上载,方法是在每个排队上载时调用#start。 然后,管理上载队列的类存储所有承诺,并使用Ember.RSVP.all处理所有承诺已解决或一个承诺已拒绝的情况。这个很好用 现在,我想取消上传 App.Upload = Ember.Object.extend({ start: function() { var self = this; return new Ember.RSVP.Promise

我需要拒绝来自外部的承诺,以处理想要取消操作的用户的情况

在这里,我需要同时启动几个上载,方法是在每个排队上载时调用#start。 然后,管理上载队列的类存储所有承诺,并使用
Ember.RSVP.all
处理所有承诺已解决或一个承诺已拒绝的情况。这个很好用

现在,我想取消上传

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构造函数包装
startUpload
?等等,不-这在逻辑上是错误的(你在这里所做的),你指望承诺只解决一次,以防止副作用,而不是实际取消上传。我建议您使用支持取消的promise库,如Bluebird。很抱歉有时间回复,谢谢您的建议。星期一我一测试完就接受!
deferred.promise.then(function(){
  console.log('all good');
},function(){
  console.log('all bad');
});