promiscuous
promiscuous is the smallest implementation of the Promises/A+ spec.
It is promise library in JavaScript, small (< 1kb minified / < 0.6kb gzipped) and fast.
Installation and usage
Node
First, install promiscuous with npm.
$ npm install promiscuous
Then, include promiscuous in your code file.
var Promise = require('promiscuous');
Browsers
Include promiscuous in your HTML file.
<script src="promicuous-browser.js"></script>
This version (and a minified one) can be built with:
$ build/build.js
API
Create a resolved promise
var one = Promise.resolve("one");
one.then(function (value) { console.log(value); });
Create a rejected promise
var none = Promise.reject(new Error("Could not keep promise."));
none.then(null, function (error) { console.error(error.message); });
Write a function that returns a promise
function promiseSomethingInASecond(something) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
if (something)
resolve(something);
else
reject(new Error("nothing"));
}, 1000);
});
}
promiseSomethingInASecond("something").then(
function (value) { console.log(value); },
function (error) { console.error(error.message); });