npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wrap-promise

v1.0.1

Published

Like new Promise(), but prevents implicit rejection

Downloads

25,357

Readme

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.