JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q61436F
  • License ISC

a Promise extension that provides filtered catch handler

Package Exports

  • smart-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 (smart-promise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Smart Promise version License

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

Build Status Downloads Code Climate Coverage Status Dependency Status Dependencies

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 --only=production --save 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:
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(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, error => {
    // Will end up here on expected everyday network errors
  })

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

©️ ahmadnassri.com  ·  License: ISC  ·  Github: @ahmadnassri  ·  Twitter: @ahmadnassri