GithubHelp home page GithubHelp logo

Comments (1)

ruanyf avatar ruanyf commented on April 30, 2024

谢谢指出,已经更正。

2015-10-24 21:58 GMT+08:00 小秦 [email protected]:

书中提供的代码:

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.

from es6tutorial.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.