Skip to content
Wraps a function and returns one that always returns a promise
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
index.js
package.json
test.js

README.md

promise-method

Build Status

js-semistandard-style

Wraps a function and returns one that always returns a promise. API-compatible with bluebird's Promise.method.

This promise will resolve with the return value of the passed function. If the passed function throws, the resulting promise will reject. The calling context is preserved in this process, so it can safely wrap object methods.

Accepts an alternate Promise implementation as input if the environment doesn't natively support them.

This module is meant to be a convenient standalone implementation of a Promise utility, but it doesn't provide an alternate Promise constructor and is less than 600 bytes minified and gzipped.

Example

var method = require('promise-method');
var foo = {
	y: 5,
	bar: method(function (x) {
		return x * this.y;
	})
}

foo.bar(6).then(result => assert(result === 30));

API

function method(fn, Promise)

fn - a synchronously executed function that may return or throw synchronously. This will be wrapped and returned as a promise-returning function.

Promise an alternate Promise implementation to use (perhaps if a global one doesn't exist)

You can’t perform that action at this time.