We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Promise对象
书中提供的代码:
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 就被触发了.
handler
readyState
resolve
见下图:
再加上后面第3节中的示例代码:
产生的效果就是: 第一个 then 被触发后, 得到了 null, 然后在 null 的基础上去取 post 属性, 从而产生异常, 打断了后面第二个then中绑定的函数.
then
null
post
至此, 我忍不住的想问一下楼主,在您编写文章的时候是否真的运行过文章中所编写的代码?
刚在Google上查了一下 Promise getJSON, 然后发现文章中的代码和rsvp中的很像, 然后又查看了一下参考链接, 发现里面果然有 rsvp.js.
Promise getJSON
rsvp
rsvp.js
可人家的 handler 中有检测 readyState, 可您的却没有.
另有一个建议, 在您文章中帖出代码的时候, 如果不是您自己的, 建议你的代码的后面附上出处哪里, 而不是放在 参考链接 中, 因为 99% 的人, 从不看 参考连接, 这就会让人们误以为,哇他好厉害,什么都知道,这代码真牛b,等等之类的想法.
参考链接
The text was updated successfully, but these errors were encountered:
谢谢指出,已经更正。
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.
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; };
见下图: [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
可人家的 handler 中有检测 readyState, 可您的却没有. [image: image] https://cloud.githubusercontent.com/assets/1265888/10710748/a66391fa-7a99-11e5-8112-04158dc20965.png
— Reply to this email directly or view it on GitHub #97.
Sorry, something went wrong.
No branches or pull requests
书中提供的代码:
handler
函数的实现上, 在进行检测是否请求完成的时候缺少对readyState
的检测,产生的后果是, 在
readyState
等于 2(第一次触发)的时候,resolve
就被触发了.见下图:
再加上后面第3节中的示例代码:
产生的效果就是: 第一个
then
被触发后, 得到了null
, 然后在null
的基础上去取post
属性, 从而产生异常, 打断了后面第二个then
中绑定的函数.至此, 我忍不住的想问一下楼主,在您编写文章的时候是否真的运行过文章中所编写的代码?
刚在Google上查了一下
Promise getJSON
, 然后发现文章中的代码和rsvp
中的很像, 然后又查看了一下参考链接, 发现里面果然有rsvp.js
.可人家的
handler
中有检测readyState
, 可您的却没有.另有一个建议, 在您文章中帖出代码的时候, 如果不是您自己的, 建议你的代码的后面附上出处哪里, 而不是放在
参考链接
中, 因为 99% 的人, 从不看 参考连接, 这就会让人们误以为,哇他好厉害,什么都知道,这代码真牛b,等等之类的想法.The text was updated successfully, but these errors were encountered: