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

smart-promise

v3.0.2

Published

a Promise extension that provides filtered catch handler

Downloads

18

Readme

Smart Promise

Smart Promise is a Promise extension that provides filtered catch handler.

license release semantic

Benchamrks

latest results

bluebird      x  3,541 ops/sec ±1.70% (82 runs sampled)
smart-promise x 99,766 ops/sec ±1.24% (86 runs sampled)

Install

npm install smart-promise

API

The same native Promise API applies, the only difference is the catch() method.

.catch(onRejected)

Behaves normally as per the native Promise API

.catch(class ErrorClass | class CustomErrorClass | ... , onRejected)

A filtered variant of catch (like other non-JS languages typically have) that lets you only handle specific errors.

The catch handler that is first met that has eligible constructors specified, is the one that will be called.

Example

Extend your existing Promise Libraries:

const { Smart } = require('smart-promise')

const Promise = Smart(MyPromiseLib)

// or, use the shorthand
const Promise = require('smart-promise')(MyPromiseLib)

Standalone:

const { Promise } = require('smart-promise')
Promise
  .then(_ => return a.b.c.d())

  .catch(TypeError, error => {
    // If the error is a "TypeError", this code block will execute
  })

  .catch(ReferenceError, error => {
    // If the error is a "ReferenceError", this code block will execute instead
  })

  .catch('TypedErrorName', error => {
    // If the error constructor matches "TypedErrorName", this code block will execute instead
  })

  .catch(error => {
  // Generic catch-the rest (error wasn't TypeError nor ReferenceError)
  })

You may also add multiple filters for a catch handler:

Promise
  .then(_ => return a.b.c.d())

  .catch(TypeError, ReferenceError, error => {
    // Will end up here on programmer error
  })

  .catch(NetworkError, TimeoutError, 'SomeError', error => {
    // Will end up here on expected everyday network errors
  })

  .catch(error => {
    // Catch any unexpected errors
  })

You can also wrap it around existing promises resolvers, this is useful for managing 3rd party generated promises:

const { Promise } = require('smart-promise')
const Library = require('some-other-promise-producting-library')

Promise.resolve(Library.action())
  .catch('SomeError', error => {})

ESlint

If you're using ESlint or similar tooling, please refer to prefer-promise-reject-errors


Author: Ahmad Nassri • Twitter: @AhmadNassri