jQuery .done()仍在deferred.resolve()之前运行

时间:2019-04-20 19:35:34

标签: jquery jquery-deferred

我在这里已经阅读了有关此主题的几个问题,但仍然看不到有什么问题。我有两个函数,一个函数从文件初始化图像并读取其尺寸,然后将它们设置为对象的变量。第二个检查尺寸是否在限制范围内。

功能:

  checkDimensions: function() {
      console.log('entering chekDimensions');
      if (options.maxDimensionsWH > 0) {

        console.log(this.checkWidth);
        console.log(this.checkHeight);
        if ((this.checkWidth <= options.maxDimensionsWH || this.checkHeight <= options.maxDimensionsWH)
             && (this.checkWidth > 0 && this.checkHeight > 0)) {
            console.log('returning true');
            return true;    
        } else {
            console.log('returning false');
            return false;
        }

    } else {
        return true;
    }
  },
  initializeCheckImage: function(file) {
    console.log('entering initialization');
    var d = $.Deferred();
    var reader = new FileReader();
      reader.onload = function (e) {
        var img = new Image;
        img.onload = function() {
          this.checkWidth = img.width;
          this.checkHeight = img.height;
          console.log('initializing the image');
          console.log(this.checkWidth);
          console.log(this.checkHeight);
          d.resolve();
        };
        console.log('assigning reader.result');
        img.src = reader.result;
      };
      console.log('assigning a file to the reader');
      reader.readAsDataURL(file);
      console.log('returning deferred');
      return d.promise();
  }

以及它们的称呼方式:

this.initializeCheckImage(file).done(check = this.checkDimensions());

从控制台上可以清楚地看到,第二个函数的执行发生在调用d.resolve();之前。

> 21:13:34.460 entering initialization
> 21:13:34.461 assigning a file to the reader
> 21:13:34.462 returning deferred
> 21:13:34.462 entering chekDimensions
> 21:13:34.462 0
> 21:13:34.463 0
> 21:13:34.463 chekDimensions returning false
> 21:13:34.478 assigning reader.result
> 21:13:34.493 initializing the image
> 21:13:34.494 30
> 21:13:34.494 30

我在做什么错?谢谢!

2 个答案:

答案 0 :(得分:1)

promise函数被立即调用:

this.initializeCheckImage(file).done(check = this.checkDimensions());

done函数应接受executer作为参数:

  

与参数resolve和reject一起传递的函数。的   执行程序功能由Promise立即执行   实现,传递解决和拒绝功能(执行器是   在Promise构造函数甚至返回创建的函数之前调用   对象)。

因此,它应该只是函数的引用,请注意,当您使用checkDimensions()调用该函数时,JS会立即执行该函数。

因此,您需要将其与函数引用一起包装,但是问题是函数的上下文已更改,并且checkDimensions()在新上下文中已不存在。

为了保持尺寸变量上下文,您可以使用以下方法从checkDimensions内部调用img.onload函数:

if (checkDimensions(img.width, img.height)) {
    return d.resolve();; 
}
else {
    return d.reject("Wrong dimensions");
}

this.initializeCheckImage(file)
    .then(() => console.log("RESOLVED"))
    .catch(() => console.log("REJECTED"));

编辑:

为了保留所需对象的上下文,您可以通过bind()使用绑定。

var readerOnload = function(e) {
    //code
}

reader.onload = readerOnload.bind(this);

或使用:

var self = this;

reader.onload = function() {
    //you can use self
}

对于您来说,imgOnload必须再次这样做。

答案 1 :(得分:0)

我建议传递该值以进行解析并在完成时接收。

  checkDimensions: function(dimensions) {
      console.log('entering chekDimensions');
      if (options.maxDimensionsWH > 0) {

        console.log(dimensions.width);
        console.log(dimensions.height);
        if ((dimensions.width <= options.maxDimensionsWH || dimensions.height <= options.maxDimensionsWH)
             && (dimensions.width > 0 && dimensions.height > 0)) {
            console.log('returning true');
            return true;    
        } else {
            console.log('returning false');
            return false;
        }

    } else {
        return true;
    }
  },
  initializeCheckImage: function(file) {
    console.log('entering initialization');
    var d = $.Deferred();
    var reader = new FileReader();
      reader.onload = function (e) {
        var img = new Image;
        img.onload = function() {
          this.checkWidth = img.width;
          this.checkHeight = img.height;
          console.log('initializing the image');
          console.log(this.checkWidth);
          console.log(this.checkHeight);
          d.resolve(
          {
             width:img.width,
             height:img.height
          });
        };
        console.log('assigning reader.result');
        img.src = reader.result;
      };
      console.log('assigning a file to the reader');
      reader.readAsDataURL(file);
      console.log('returning deferred');
      return d.promise();
  }

然后

this.initializeCheckImage(file).done(function(dimensions) 
{ 
check = this.checkDimensions(dimensions) 
});