Bare bones Promises/A+ implementation
This is a simple implementation of Promises. It is a super set of ES6 Promises designed to have readable, performant code and to provide just the extensions that are absolutely necessary for using promises today.
For detailed tutorials on its use, see www.promisejs.org
N.B. This promise exposes internals via underscore (
_) prefixed properties. If you use these, your code will break with each new release.
$ npm install promise
You can use browserify on the client, or use the pre-compiled script that acts as a polyfill.
Note that the es5-shim must be loaded before this library to support browsers pre IE9.
The example below shows how you can load the promise library (in a way that works on both client and server using node or browserify). It then demonstrates creating a promise from scratch. You simply call
new Promise(fn). There is a complete specification for what is returned by this method in Promises/A+.
var Promise = require'promise';var promise =get''if err rejecterr;else resolveres;;;
If you need domains support, you should instead use:
var Promise = require'promise/domains';
If you are in an environment that implements
setImmediate and don't want the optimisations provided by asap, you can use:
var Promise = require'promise/setimmediate';
If you only want part of the features, e.g. just a pure ES6 polyfill:
var Promise = require'promise/lib/es6-extensions';// or require('promise/domains/es6-extensions');// or require('promise/setimmediate/es6-extensions');
Before all examples, you will need:
var Promise = require'promise';
This creates and returns a new promise.
resolver must be a function. The
resolver function is passed two arguments:
resolveshould be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A).
rejectshould be called with a single argument. The returned promise will be rejected with that argument.
These methods are invoked by calling
Converts values and foreign promises into Promises/A+ promises. If you pass it a value then it returns a Promise for that value. If you pass it something that is close to a promise (such as a jQuery attempt at a promise) it returns a Promise that takes on the state of
value (rejected or fulfilled).
Returns a promise for an array. If it is called with a single argument that
Array.isArray then this returns a promise for a copy of that array with any promises replaced by their fulfilled values. e.g.
PromiseallPromiseresolve'a' 'b' Promiseresolve'c'thenassertres0 === 'a'assertres1 === 'b'assertres2 === 'c'
Takes a function which accepts a node style callback and returns a new function that returns a promise instead.
var fs = require'fs'var read = PromisedenodeifyfsreadFilevar write = PromisedenodeifyfswriteFilevar p = read'foo.json' 'utf8'thenreturn write'foo.json' JSONstringifyJSONparsestr null ' ' 'utf8'
The twin to
denodeify is useful when you want to export an API that can be used by people who haven't learnt about the brilliance of promises yet.
moduleexports = PromisenodeifyawesomeAPIreturn downloada b
If the last argument passed to
module.exports is a function, then it will be treated like a node.js callback and not parsed on to the child function, otherwise the API will just return a promise.
These methods are invoked on a promise instance by calling
This method follows the Promises/A+ spec. It explains things very clearly so I recommend you read it.
onRejected will be called and they will not be called more than once. They will be passed a single argument and will always be called asynchronously (in the next turn of the event loop).
If the promise is fulfilled then
onFulfilled is called. If the promise is rejected then
onRejected is called.
The call to
.then also returns a promise. If the handler that is called returns a promise, the promise returned by
.then takes on the state of that returned promise. If the handler that is called returns a value that is not a promise, the promise returned by
.then will be fulfilled with that value. If the handler that is called throws an exception then the promise returned by
.then is rejected with that exception.
Promise#then(null, onRejected), to mirror
catch in synchronous code.
The same semantics as
.then except that it does not return a promise and any exceptions are re-thrown so that they can be logged (crashing the application in non-browser environments)
undefined it just returns
callback is a function it is called with rejection reason as the first argument and result as the second argument (as per the node.js convention).
This lets you write API functions that look like:
return internalAPIfoo barthenparseResultthennull retryErrorsnodeifycallback
People who use typical node.js style callbacks will be able to just pass a callback and get the expected behavior. The enlightened people can not pass a callback and will get awesome promises.