make-promise

A function that creates a Promises/A+ promise.

npm install make-promise
10 downloads in the last day
77 downloads in the last week
202 downloads in the last month

make-promise Build Status Dependency Status

A function which creates a Promises/A+ promise.

This module passes version 1.3.1 of the Promises/A+ Compliance Test Suite.

Usage

make-promise exports a single function, which takes a function as it sole argument. After you have called makePromise, this function is called immediately afterwards with a Node-style callback as its sole argument.

  • Calling the callback with a truthy error argument will cause the promise to be rejected with the value of the error argument.
  • Calling the callback with a falsy error argument will cause the promise to be fulfilled with the value of the result argument. If there is no result argument, the promise will be fulfilled with undefined.

Code example

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

Credits

This implementation is based on the code for the "promise" module by Forbes Lindsay. See credits.txt.

License

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

npm loves you