Skip to content

This a extending code with some modification from [Promises/A+]

License

Notifications You must be signed in to change notification settings

peterlevel1/promise-then

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This a extending code with some modification from Promises/A+ implementation.

It is designed to get the basics spot on correct, so that you can build extended promise implementations on top of it.

API

var Promise = require('./promise');

var promiseSomeTask = new Promise(function (resolve, reject) {
    get('http://www.baidu.com', function (err, res) {
      if (err) reject(err);
      else resolve(res);
    });
  });

Inheritance

You can use inheritance if you want to create your own complete promise library with this as your basic starting point, perfect if you have lots of cool features you want to add. Here is an example of a promise library called Awesome, which is built on top of Promise correctly.

var Promise = require('./promise');
var factory = Promise.factory;

function Awesome(name) {
  if (!(this instanceof Awesome)) return new Awesome(fn);
  this.name = name;
  this.then = factory(name).then;
}

//Awesome extension
Awesome.prototype.spread = function (cb) {
  return this.then(function (arr) {
    return cb.apply(this, arr);
  })
};

N.B. if you fail to set the prototype and constructor properly or fail to do Promise.call, things can fail in really subtle ways.

License

MIT

About

This a extending code with some modification from [Promises/A+]

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published