JSPM

wrap-promise

0.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3921
  • Score
    100M100P100Q118685F

Like new Promise(), but prevents implicit rejection

Package Exports

  • wrap-promise

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (wrap-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

wrap-promise

Build Status Build status Coverage Status Dependency Status devDependency Status

Like new Promise(), but prevents implicit rejection

Comparison

Using the native new Promise()

var fs = require('fs');

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

  fs.readFile(123, function(err, buf) { // doesn't throw, but calls `onRejected` function
    if (err) {
      reject(err);
      return;
    }
    resolve(buf);
  });
}).catch(onRejected);

function onRejected() {
  console.log('This function should be called.');
}

Using wrap-promise

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

wrapPromise(function(resolve, reject) {
  fs.readFile(123, function(err, buf) { // doesn't call `onRejected` but throws immediately
    if (err) {
      reject(err);
      return;
    }
    resolve(buf);
  });
}).catch(onRejected);

function onRejected() {
  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

var 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)

var 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 Shinnosuke Watanabe

Licensed under the MIT License.