Comments (1)
谢谢指出,已经更正。
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)
- 页面无内容 HOT 1
- es6文档显示不了了 HOT 1
- 找不到网页内容 HOT 1
- edge 显示 file not found HOT 1
- Proxy章节描述错误 HOT 1
- 如果按照文档所说,协程应该是并发执行的,期望解读 HOT 1
- 18、Iterator 和 for...of 循环 HOT 1
- 每年新增的属性可以有一个列表,之后链接到对应的文章么 HOT 1
- Module 的语法 HOT 1
- 请问哪个js环境支持simd HOT 1
- Generator 的语法一章的wrapper包装例子是不是可以简化 HOT 1
- 建议「Module 的语法」这章里可以增加一个说明,就是:“export 可以输出什么”
- 关于Promise.resolve()是在本轮“事件循环”(event loop)的结束时执行,还是在下一轮“事件循环”的开始时的疑问 HOT 1
- array grouyBy是写错了么 HOT 2
- 关于 WeakSet WeakMap 的使用表现与文档不一致的问题 HOT 3
- Promise 对象一章关于 finally 的实现的问题 HOT 1
- 关于装饰器章节这个说法的疑问:「装饰器对类的行为的改变,是代码编译时发生的,而不是在运行时」 HOT 1
- 对象的Symbol.toStringTag属性,指向一个方法。这句“指向一个方法”是否是错误呢? HOT 1
- https://es6.ruanyifeng.com/#docs/proxy 中容易引起误解的部分
- 文档中的链接需要更新,原链接已经404 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from es6tutorial.