A straight-forward Promises/A+ 1.1 implementation
Pinky is a no-nonsense Promises/A+ 1.1 implementation. Pinky is written to be very readable and easy to follow, with references to the relevant sections of the spec for each operation. As such, Pinky can be used as an academic example of a promises implementation.
Pinky includes a number of fully documented examples that illustrate common use cases, explain exactly what's going on, and highlight the power of promises. Several included examples can be used as independent explanations of the functionality promises can provide without any prior introduction to the concept.
Create a new Pinky instance with var pinky = new Pinky()
.
Used to add onFulfilled and onRejected callbacks to the promise. The same method is provided as pinky.promise.then(...)
.
When passed a value, the promise will be fulfilled. All onFulfilled callbacks will receive the passed value as their first argument. pinky.resolve
is aliased to this method.
When passed a reason, the promise will be rejected. All onRejected callbacks will receive the passed error as their first argument.
The pinky.promise
property is a "thenable" object that should be returned by functions that use Pinky. Instead of returning the Pinky instance itself, which would allow callers to fulfill/reject the promise, your function should return pinky.promise
-- an object that includes the one method a promise must have: pinky.then(...)
.
Pinky can be used on both the server and the client.
First, install the pinkypromise
module using npm
. Optionally use --save
to save Pinky as a dependency in your package.json
:
npm install pinkypromise --save
Next, require Pinky:
var pinky = require('pinky');
Then, use promises in your code:
function helloWorld() {
var pinky = new Pinky();
pinky.fulfill('world');
return pinky.promise;
}
helloWorld().then(function(value) {
console.log('Hello '+value);
});
Just include pinky.js
on your page.
Pinky can also be used as an AMD module.
Examples for Node.js and the browser are available in the examples/
folder.
Some of the examples located in examples/browser/
fetch files with XMLHttpRequest, and most browsers prevent local files from being fetched in this way. There are a number of different ways to run examples locally, the most straightforward of which is to run python -m SimpleHTTPServer
or python -m http.server
and navigate to http://127.0.0.1:8000.
Execute the following commands to run the Promises/A+ test suite:
npm install
npm test
BSD license, Copyright © 2014 Lawrence Davis