Skip to content

liwenlong/promiseA

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

promiseA

A simple Promises/A+ implementation.

Useage

// way 1
var read = function(path) {
    var p = Promise();
    
    fs.readFile(path, function(err, data) {
        if(!err) p.resolve(data);
        else p.reject(err);
    });
    
    return p;
};

// way 2
var write = function(data) {
    return Promise(function(resolve, reject) {
        fs.writeFile('bar.js', function(err) {
            if(!err) resolve();
            else reject(err);
        });
    });
};

// way 3
var sleep = function(ms) {
    return function(v) {
        var p = Promise();

        setTimeout(function() {
            p.resolve(v);
        }, ms);

        return p;
    };
};

read('foo.js')
    .then(sleep(1000))
    .then(write)
    .then(sleep(1000))
    .then(function() {
        console.log('done!');
    });

API list

constructor

  • Promise()

instance methods

  • then()

  • resolve()

  • reject()

static methods

  • Promise.resove()

  • Promise.reject()

  • Promise.all()

  • Promise.any()

Idea About Promise

JavaScript Promise

About

A simple Promises/A+ implementation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 98.4%
  • HTML 1.6%