Hahazhu

Javascript Promise Implementation

var Promise=function(args)
{
  this.callback=[];
  var _this=this;
  setTimeout(function() {
    _this.done();
  }, 0);
};
Promise.prototype.then=function(result)
{
  this.callback.push(result);
  return this;
};
Promise.prototype.when=function()
{
  throw new Error('Not implemented');
};
Promise.prototype.done=function()
{
  console.log(this.callback.join(','));
  return this;
};

new Promise().then(10).then(11).then(12);

 

posted @ 2014-12-04 19:06  hahazhu  阅读(151)  评论(0)    收藏  举报