Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

es6-promise-extended

1.1.1 • Public • Published

es6-promise-extended

A lightweight extension to the es6-promise library, which brings a finally clause and deferred promises, alongside the es6-promise Promise object and static methods.

Building and testing

  • npm install To install all dependencies
  • npm test To run the tests
  • npm run watch-tests To run the tests and watch the code for changes
  • npm run lint To lint the code

Usage

To install:

npm install es6-promise-extended

To use:

var ExtendedPromise = require('es6-promise-extended');

Promise

Promises are created as shown:

var promise = new ExtendedPromise.Promise(function (resolve, reject) {
    resolve('someValue');
    // OR
    reject('someError');
});

finally

Finally is always called after the rejection or fulfillment of a promise, as shown:

var promise = new ExtendedPromise.Promise(function (resolve, reject) {
    resolve(10);
});
 
promise
    .then(function (result) {
        return result * 2;
    })
    .finally(function (result) {
        console.log('Finally!', result);
    });
 
// Finally! 20
var promise = new ExtendedPromise.Promise(function (resolve, reject) {
    reject();
});
 
promise
    .catch(function (result) {
        return 'Error!';
    })
    .finally(function (result) {
        console.log('Finally!', result);
    });
 
// Finally! Error!

defer

defer allows you to pass the control of the promise onto a consumer, who can control when to resolve or reject the promise, as shown:

Resolve
var deferred = ExtendedPromise.defer();
 
deferred.promise
    .then(function (msg) {
        console.log('Resolved!', msg);
    });
 
deferred.resolve('yo!');
 
// Resolved! yo!
 
Reject
var deferred = ExtendedPromise.defer();
 
deferred.promise
    .catch(function (msg) {
        console.log('Rejected!', msg);
    });
 
deferred.reject('Something went wrong..');
 
// Rejected! Something went wrong..
 

Finally

It is compatible with the finally method too:

var deferred = ExtendedPromise.defer();
 
deferred.promise
    .finally(function (msg) {
        console.log('Finally!', msg);
    });
 
deferred.resolve('I always run.');
 
// Finally! I always run.

Promise.all

all resolves when all the promises passed to it have resolved or rejected

var promise1 = new ExtendedPromise.Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(10);
    }, 1);
});
var promise2 = new ExtendedPromise.Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(20);
    }, 2);
});
 
ExtendedPromise.all([ promise1, promise2 ].then(function (result) {
    console.log('Resolved!', result[0], result[1]);
});
 
// Resolved! 10 20

Promise.race

race resolves as soon as a promise passed to it has been resolved or rejected

var promise1 = new ExtendedPromise.Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(10);
    }, 1);
});
var promise2 = new ExtendedPromise.Promise(function (resolve, reject) {
    setTimeout(function () {
        resolve(20);
    }, 2);
});
 
ExtendedPromise.race([ promise1, promise2 ]).then(function (result) {
    console.log('Resolved!', result);
});
 
// Resolved! 10

Promise.resolve

Returns a promise that is resolved

var promise = ExtendedPromise.resolve(10);
 
promise.then(function (result) {
    console.log('Resolved!', result);
});
 
// Resolved! 10

Promise.reject

Returns a promise that is rejected

var promise = ExtendedPromise.reject('I have been rejected!');
 
promise.catch(function (msg) {
    console.log('Error!', msg);
});
 
// Resolved! I have been rejected!

Install

npm i es6-promise-extended

DownloadsWeekly Downloads

0

Version

1.1.1

License

MIT

Last publish

Collaborators

  • avatar