首页 > 其他 > 详细

简易promise

时间:2014-04-04 21:36:26      阅读:492      评论:0      收藏:0      [点我收藏+]

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
/**
* Created by addison on 2014/4/4.
*/
//简易promise
(function (w) {

var a = {};
a.get = function (url) {
return new a.promise(a.ajax, url);
}

//简易ajax
a.ajax = function (url) {
var me = this;
var request = new XMLHttpRequest();

request.open(‘GET‘, url);
request.onload = function () {
if (request.status == 200) {
me.success(request.responseText);
} else {
me.success(request);
}
};

request.onerror = function () {
reject(Error(‘Error fetching data.‘)); // error occurred, reject the Promise
};

request.send(); //send the request

}

a.promise = function (promise, url) {

promise.call(this, url);
}
a.promise.prototype.then = function (success, error) {
this.success = success;
this.error = error;
}

window.a = a;

}(window))
</script>
<script>
a.get(‘json or text‘).then(
function success(data) {
document.write(data);
}, function error() {
debugger;
})
</script>

</head>
<body>

</body>
</html>

简易promise,布布扣,bubuko.com

简易promise

原文:http://www.cnblogs.com/Mr-Joe/p/3645551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!