promiscuous
A minimal and fast promise implementation
Last updated 8 years ago by rubenverborgh .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install promiscuous 
SYNC missed versions from official npm registry.

promiscuous

Promises/A+ logo

promiscuous is a tiny 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="promiscuous-browser.js"></script>

This version (and a minified one) can be built with:

$ build/build.js

API

Create a resolved promise

var promise = Promise.resolve("one");
promise.then(function (value) { console.log(value); });
/* one */

Create a rejected promise

var brokenPromise = Promise.reject(new Error("Could not keep promise."));
brokenPromise.then(null, function (error) { console.error(error.message); });
/* "Could not keep promise." */

You can also use the catch method if there is no success callback:

brokenPromise.catch(function (error) { console.error(error.message); });
/* "Could not keep promise." */

Write a function that returns a promise

function promiseLater(something) {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      if (something)
        resolve(something);
      else
        reject(new Error("nothing"));
    }, 1000);
  });
}
promiseLater("something").then(
  function (value) { console.log(value); },
  function (error) { console.error(error.message); });
/* something */

promiseLater(null).then(
  function (value) { console.log(value); },
  function (error) { console.error(error.message); });
/* nothing */

Convert an array of promises into a promise for an array

var promises = [promiseLater(1), promiseLater(2), promiseLater(3)];
Promise.all(promises).then(function (values) { console.log(values); });
/* [1, 2, 3] */

Current Tags

  • 0.7.2                                ...           latest (8 years ago)

10 Versions

  • 0.7.2                                ...           8 years ago
  • 0.7.1                                ...           9 years ago
  • 0.7.0                                ...           9 years ago
  • 0.6.0                                ...           12 years ago
  • 0.5.1                                ...           12 years ago
  • 0.5.0                                ...           12 years ago
  • 0.4.0                                ...           12 years ago
  • 0.3.0                                ...           13 years ago
  • 0.2.0                                ...           13 years ago
  • 0.1.0                                ...           13 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |