New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

maybe-promise-factory

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

maybe-promise-factory

Calls a function and wraps the retval or exception in a Promise only if it didn't already return a Promise; like Twisted's maybeDeferred. Turns a sync-or-Promise API into a Promise API.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

maybe-promise-factory

Calls a function and wraps the retval or exception in a Promise only if it didn't already return a Promise; like Twisted's maybeDeferred. Turns a sync-or-Promise API into a Promise API.

Installation

Install with npm install maybe-promise-factory.

Usage

const maybePromise = require('maybe-promise-factory')(Promise);

function syncSum (a, b) {
  if (typeof a !== 'number' || typeof b !== 'number') {
    throw new TypeError('Invaild arguments passed to syncSum');
  }
  return a + b;
}

function asyncSum (a, b) {
  return new Promise((resolve, reject) => {
    if (typeof a !== 'number' || typeof b !== 'number') {
      reject(new TypeError('Invalid arguments passed to asyncSum'));
      return;
    }
    resolve(a + b);
  });
}

maybePromise(syncSum, 1, 2).then(res => console.log(res)); // 3
maybePromise(syncSum, 1, 'x').catch(err => console.error(err)); // TypeError

maybePromise(asyncSum, 1, 2).then(res => console.log(res)); // 3
maybePromise(asyncSum, 1, 'x').catch(err => console.error(err)); // TypeError

maybePromiseFactory

The maybePromiseFactory accepts two arguments:

  • {class} promiseConstructor A Promise implementation (e.g. Bluebird or Promise)
  • {boolean} [useAnyThenable] If truthy, any Promise implementation is considered a promise; otherwise, only instances of the promiseConstructor are considered promises.

The maybePromiseFactory returns a maybePromise function, which is passed a function (synchronous or asynchronous) and any parameters to invoke it with. It returns an instance of promiseConstructor.

Keywords

FAQs

Package last updated on 16 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc