make-promise

A function that creates a Promises/A+ promise.

make-promise

A function which creates a Promises/A+ promise.

This module passes version 1.3.1 of the Promises/A+ Compliance Test Suite but is not an implementation of the spec itself. It simply delegates everything to a proper Promises/A+ implementation. Currently, this is Promiscuous by Ruben Verborgh.

I noticed that I wanted a simple way to create a promise in multiple projects. I already had written a makePromise function for Faithful, but I did not want to introduce a dependency on the whole Faithful library.

This module effectively serves as a convenient interface which I can write to, and which abstracts away the specific implementation method, so I can choose another one when this seems useful.

make-promise exports a single function, which takes a function as it sole argument. After you have called makePromise, the function you have provided is immediately called afterwards with two paramaters; resolve and reject. Calling resolve at some point will cause the promise to be fulfilled, calling reject at some point will cause the promise to fail.

var skyIsFalling
makePromise = require("make-promise")
promise = makePromise(function(resolvereject) {
  setImmediate(function() {
    if skyIsfalling
      reject(new Error("Sky is falling."))
    else
      resolve()
  })
})
promise.then(
  function(){ 
    // phew!  
  }, 
  function(err) { 
    console.error err.toString() + " Better take cover."
  })

Absolutely everything is done by Promiscuous, a Promises/A+ implementation created by Ruben Verborgh.

The idea for the specific API is not mine either. It mimics the API of the (upcoming) RSVP 2.0. The only difference is that RSVP uses new RSVP.Promise and in mine its just makePromise.

make-promise is released under the MIT License.
Copyright (c) 2013 Meryn Stol