Skip to content

Commit

Permalink
fixbug of promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ccforward committed Apr 19, 2017
1 parent d4147ba commit 1d94bde
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
12 changes: 5 additions & 7 deletions promise/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
<script>

function start () {
var p = new Promise();
console.log('start');
p.resolve();
return p;
return new Promise().resolve();
}

function test1() {
Expand All @@ -26,7 +24,7 @@
setTimeout( function(){
console.log('sleep1');
p.resolve();
}, 200);
}, 2000);

return p;
}
Expand Down Expand Up @@ -69,9 +67,9 @@
start().then( test1, null )
.then( sleep1(), null )
.then( test2, null )
.then( sleep2(), null )
.then( test3, null )
.then( sleep3(), null );
// .then( sleep2(), null )
// .then( test3, null )
// .then( sleep3(), null );

</script>
</body>
Expand Down
26 changes: 9 additions & 17 deletions promise/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,11 @@
}

/*
*给一个promise添加成功和失败回调函数
* 给一个promise添加成功和失败回调函数
* @param {Function}
* @param {Function}
* @return {Obejct} 返回一个Promise对象
**/
// Promise.prototype = {
// constructor: Promise,
// then: function(resolve, reject){

// },
// resolve: function(){

// },
// reject: function(){

// }
// }
Promise.prototype.then = function(resolve, reject) {

if (resolve instanceof global.Promise) return resolve;
Expand Down Expand Up @@ -79,6 +67,8 @@
this._resolves[i].apply(null);
}

return this;

};

/*
Expand All @@ -93,23 +83,25 @@
for (; i < len; i++) {
this._rejects[i].apply(null);
}

return this;
};

global.Promise = Promise;


/****************功能函数*********************/
function proxy(fun, context) {
var source = context || this;
var _self = context || this;

return fun.bind ? fun.bind(source) : function() {
fun.apply(source, arguments);
return fun.bind ? fun.bind(_self) : function() {
fun.apply(_self, arguments);
};
}

function type(obj) {
var o = {};
return o.toString.call(obj).replace(/^\[ Object (\w+)\]$/, '$1').toLowerCase();
return Object.toString.call(obj).replace(/^\[ Object (\w+)\]$/, '$1').toLowerCase();
}

function isFunc(obj) {
Expand Down

0 comments on commit 1d94bde

Please sign in to comment.