0.0.2 • Published 7 years ago

pretty-promise v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

pretty-promise

Build Status Standard - JavaScript Style Guide

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