Skip to content

Create rho-contracts for promises

License

MPL-2.0, MIT licenses found

Licenses found

MPL-2.0
LICENSE
MIT
LICENSE.alternate
Notifications You must be signed in to change notification settings

rho-contracts/rho-cc-promise

Repository files navigation

rho-cc-promise

version license build code style

Create rho-contracts for Promises.

Usage

Use with @rho-contracts/rho-contracts:

  "dependencies": {
    "@rho-contracts/rho-contracts": "^3.0.0"
  }

Use this module to create contracts for promises. The returned contracts check the values of resolve and optionally reject, propagating contract errors through reject.

Calling withError on the returned contract changes the type of the error argument to the contract specified.

The main entry point of this module is a mixin, which extends c.fn and c.fun to return contracts with returnsPromise methods.

const c = require('rho-cc-promise').mixin(require('rho-contracts-fork'))

const cc = {}

cc.info = c.object({ data: c.any })

cc.getInfo = c.fun().returnsPromise(cc.info)

// A function which returns a promise which resolves with no value.
cc.doSomething = c.fun().returnsPromise(c.value(undefined))

// A function which returns a promise which rejects with a custom error type.
cc.customError = c.array(c.error)

cc.doSomething = c
  .fun()
  .returnsPromise(c.value(undefined))
  .withError(cc.customError)

It's compatible with most promise implementations, including ES6 Promise and implementations which adhere to A+.

Installation

npm install rho-contracts-fork rho-cc-promise

Contribute

Pull requests welcome!

License

The project is dually licensed under the Mozilla Public License Version 2.0, and the MIT license. You may use either one, at your option.