Skip to content

Commit

Permalink
Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyWenjing committed Aug 9, 2016
1 parent 3ab2d09 commit 45b7209
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions es6/promise/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value":"hello"
}
38 changes: 38 additions & 0 deletions es6/promise/promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function timeout (ms) {
return new Promise ((resolve,reject) => {
setTimeout(resolve,ms,'done');
});
}

timeout(100).then ((value) => {
console.log(value);
});

//用Promise实现Ajax

var getJSON = function (url) {
var promise = new Promise (function (resolve,reject){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = handler;
xhr.open("GET",url);
// xhr.responseType = 'json';
// xhr.setRequestHeader("Accept","application/json");
xhr.send();
function handler () {
if (this.readyState == 4) {
if (this.status >= 200 && this.status < 300 || this.status == 304) {
resolve(this.responseText);
} else {
reject(new Error (this.statusText));
}
}
};;
});
return promise;
};

getJSON("../../../es6/es6/promise/data.json").then(function (json) {
console.log("sucess:" + json);
},function (error) {
console.log("error:" + error);
});
File renamed without changes.

0 comments on commit 45b7209

Please sign in to comment.