simon-promise

a Promise extension that provides filtered catch handler

Usage no npm install needed!

<script type="module">
  import simonPromise from 'https://cdn.skypack.dev/simon-promise';
</script>

README

simon version License

Simon is a Promise extension that provides filtered catch handler ... made for Simon.

Build Status Downloads Code Climate Coverage Status Dependencies

Install

npm install --production --save simon-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:
somePromise
  .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:

somePromise
  .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
  })

:copyright: www.ahmadnassri.com  ·  License: ISC  ·  Github: @ahmadnassri  ·  Twitter: @ahmadnassri