Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wrap-promise

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wrap-promise

Like new Promise(), but prevents implicit rejection

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
increased by2.22%
Maintainers
1
Weekly downloads
 
Created
Source

wrap-promise

NPM version Bower version Build Status Build status Coverage Status Dependency Status devDependency Status

Like new Promise(), but prevents implicit rejection

Comparison

Using the native new Promise()

const fs = require('fs');

new Promise((resolve, reject) => {
  // Node's fs.readFile throws a type error when the first argument is not a string.

  fs.readFile(123, (err, buf) => { // doesn't throw, but calls `onRejected` function
    if (err) {
      reject(err);
      return;
    }
    resolve(buf);
  });
}).catch(() => console.log('This function should be called.'));

Using wrap-promise

const fs = require('fs');
const wrapPromise = require('wrap-promise');

wrapPromise((resolve, reject) => {
  fs.readFile(123, (err, buf) => { // doesn't call `onRejected` but throws immediately
    if (err) {
      reject(err);
      return;
    }
    resolve(buf);
  });
}).catch(() => console.log('This function should not be called.'));

According to the Promise specification, a promise will be rejected implicitly when an error is thrown in the constructor callback. The only (and the biggest) difference is that wrap-promise immediately throws an error in such a case.

Installation

Package managers

npm
npm install wrap-promise
Bower
bower install wrap-promise
Duo
const wrapPromise = require('shinnn/wrap-promise');

Standalone

Download the script file directly.

API

wrapPromise(fn)

fn: Function
Return: Object (Promise)

It can be used in the same way as new Promise() but new operator is not needed.

wrapPromise.Promise

Type: Function
Default: global Promise or require('es6-promise').Promise

The Promise constructor used in wrapPromise function.

On CommonJS-based environment (e.g. Node)

By default it uses the global Promise constructor if available, otherwise it requires es6-promise and use its Promise property.

If you don't need the fallback, use no-fallback.js instead. (Useful for Browserify)

const wrapPromise = require('wrap-promise/no-fallback');
On non-CommonJS environment

It uses the global Promise constructor without any fallbacks. Before using wrapPromise, you must load Promise polyfill if Promise doesn't exist by default.

License

Copyright (c) 2014 - 2015 Shinnosuke Watanabe

Licensed under the MIT License.

Keywords

FAQs

Package last updated on 13 Jun 2015

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