code test
Promise = function() {
this.check = [];
this.resolved = false;
};
Promise.prototype = {
success: function(callback){
if (this.resolved) {
callback(this.results);
} else {
this.check.push(callback);
}
},
resolve: function(result){
if(this.resolved) {
throw new Error('promise resolved');
}
this.results = result;
for (var i = 0; i < this.check.length; i++) {
this.check[i](result);
}
this.resolved = true;
}
};
var foo = new Promise(),
bar = new Promise();
foo.resolve('hello');
setTimeout(function(){
bar.resolve('world');
}, 500);
foo.success(function(result){
console.log(result);
});
bar.success(function(result){
console.log(result);
});
setTimeout(function() {
bar.resolve('world');
}, 500);
setTimeout(function() {
var far = new Promise();
far.success(function(result) {
console.log(result);
});
far.success(function(result) {
console.log(result + ' 1');
});
far.resolve('weeee');
}, 500);