Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

本书第16节 Promise对象 中的一个问题 #97

Closed
xqin opened this issue Oct 24, 2015 · 1 comment
Closed

本书第16节 Promise对象 中的一个问题 #97

xqin opened this issue Oct 24, 2015 · 1 comment

Comments

@xqin
Copy link
Contributor

xqin commented Oct 24, 2015

书中提供的代码:

var getJSON = function(url) {
  var promise = new Promise(function(resolve, reject){
    var client = new XMLHttpRequest();
    client.open("GET", url);
    client.onreadystatechange = handler;
    client.responseType = "json";
    client.setRequestHeader("Accept", "application/json");
    client.send();

    function handler() {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(new Error(this.statusText));
      }
    };
  });

  return promise;
};

handler 函数的实现上, 在进行检测是否请求完成的时候缺少对readyState的检测,
产生的后果是, 在 readyState 等于 2(第一次触发)的时候, resolve 就被触发了.

见下图:
image

再加上后面第3节中的示例代码:
image

产生的效果就是: 第一个 then 被触发后, 得到了 null, 然后在 null 的基础上去取 post 属性, 从而产生异常, 打断了后面第二个then中绑定的函数.

至此, 我忍不住的想问一下楼主,在您编写文章的时候是否真的运行过文章中所编写的代码?

刚在Google上查了一下 Promise getJSON, 然后发现文章中的代码和rsvp中的很像, 然后又查看了一下参考链接, 发现里面果然有 rsvp.js.

可人家的 handler 中有检测 readyState, 可您的却没有.
image

另有一个建议, 在您文章中帖出代码的时候, 如果不是您自己的, 建议你的代码的后面附上出处哪里, 而不是放在 参考链接 中, 因为 99% 的人, 从不看 参考连接, 这就会让人们误以为,哇他好厉害,什么都知道,这代码真牛b,等等之类的想法.

@ruanyf
Copy link
Owner

ruanyf commented Oct 25, 2015

谢谢指出,已经更正。

2015-10-24 21:58 GMT+08:00 小秦 notifications@github.com:

书中提供的代码:

var getJSON = function(url) {
var promise = new Promise(function(resolve, reject){
var client = new XMLHttpRequest();
client.open("GET", url);
client.onreadystatechange = handler;
client.responseType = "json";
client.setRequestHeader("Accept", "application/json");
client.send();

function handler() {
  if (this.status === 200) {
    resolve(this.response);
  } else {
    reject(new Error(this.statusText));
  }
};

});

return promise;
};

handler 函数的实现上, 在进行检测是否请求完成的时候缺少对readyState的检测,
产生的后果是, 在 readyState 等于 2(第一次触发)的时候, resolve 就被触发了.

见下图:
[image: image]
https://cloud.githubusercontent.com/assets/1265888/10710705/58d36836-7a97-11e5-99f9-6deed2c2bb79.png

再加上后面第3节中的示例代码:
[image: image]
https://cloud.githubusercontent.com/assets/1265888/10710709/c52f59f4-7a97-11e5-9163-a51c351daa40.png

产生的效果就是: 第一个 then 被触发后, 得到了 null, 然后在 null 的基础上去取 post 属性, 从而产生异常,
打断了后面第二个then中绑定的函数.

至此, 我忍不住的想问一下楼主,在您编写文章的时候是否真的运行过文章中所编写的代码?

刚在Google上查了一下 Promise getJSON, 然后发现文章中的代码和rsvp中的很像, 然后又查看了一下参考链接, 发现里面果然有
rsvp.js.

可人家的 handler 中有检测 readyState, 可您的却没有.
[image: image]
https://cloud.githubusercontent.com/assets/1265888/10710748/a66391fa-7a99-11e5-8112-04158dc20965.png

另有一个建议, 在您文章中帖出代码的时候, 如果不是您自己的, 建议你的代码的后面附上出处哪里, 而不是放在 参考链接 中, 因为 99% 的人,
从不看 参考连接, 这就会让人们误以为,哇他好厉害,什么都知道,这代码真牛b,等等之类的想法.


Reply to this email directly or view it on GitHub
#97.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants