Skip to content

wbinnssmith/promise-try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-try

Build Status

js-semistandard-style

Wraps a synchronously executing function and returns a promise that resolves to its return value and rejects its exceptions. API-compatible with Bluebird's Promise.try.

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 500 bytes minified and gzipped.

Example

var promiseTry = require('promise-try');

promiseTry(function () {
	return 5;
}).then(result => assert(result === 5));

API

function promiseTry(fn, args, ctx, Promise)

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

args - arguments to applied to the passed function. If not an array, it will be passed as a single argument to the function. If an array, it will be spread across the function's parameters.

I personally don't like this API since one could never achieve passing the function a single argument that is an array, but this is here for compatibility with bluebird. Instead, one could just use something like .bind(null, [1, 2, 3]) when passing the function.

ctx - the context (this) to be used when the function is executed.

Again, one could just use .bind(ctx) on the original function

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

About

Wrap a synchronously executing function and return a promise

Resources

License

Stars

Watchers

Forks

Packages

No packages published