pretty-promise

0.0.2 • Public • Published

pretty-promise

Build Status Standard - JavaScript Style Guide

Promises/A+ logo

Small and lightweight implementation of Promises A/+.

var pp = require('pretty-promise')
 
var promise = pp(function (resolve, reject) {
  someAsyncFunction(function (err, result) {
    if (err) return reject(err)
    resolve(result)
  })
})
 
promise
.then(function onFulfilled (value) {
  console.log(value)
})
.catch(function onRejected (reason) {
  console.log(reason)
})

installation

npm install pretty-promise --save

tests

Pretty Promise uses the Promises A/+ Compliance Tests.

Before you run the tests, you need to install all devDependencies:

npm install

Then, just run:

npm test

api

pp(resolver) -> Promise

You can create promises with a resolver function:

function myAsyncFunc () {
  return pp(function (resolve, reject) {
    // ...
 
    if (!err) {
      resolve(value)
    } else {
      reject(err)
    }
  })
}

Or you can use a Defer-like syntax:

function myAsyncFunc () {
  var promise = pp()
 
  // ...
 
  if (!err) {
    promise.resolve(value)
  } else {
    promise.reject(err)
  }
 
  return promise
}

pp#then(onFulfilled, onRejected) -> Promise

Add the promise's handlers.

pp#catch(onRejected) -> Promise

A shortcut for promise.then(null, onRejected).

todo

  • Implements Promises A/+ spec.
  • Implements catch(onRejected) alias.
  • Implements tp.when() static method.

license

MIT

Package Sidebar

Install

npm i pretty-promise

Weekly Downloads

0

Version

0.0.2

License

MIT

Last publish

Collaborators

  • gsantiago