Git Product home page Git Product logo

catcher's Introduction

@isaacs/catcher

Call a function and return a fallback value or undefined if it throws.

Cuts out the performance overhead of creating a stack trace, for cases where it won't be used anyway.

USAGE

import { catcher, catchWrap } from '@isaacs/catcher'
import { functionThatMightThrow } from 'some-module'

const resultOrUndefined = catcher(functionThatMightThrow)
const resultOr99 = catcher(functionThatMightThrow, 99)

const functionThatDoesNotThrow = catchWrap(functionThatMightThrow)
// this returns undefined rather than throwing
const resultOrUndefined = functionThatDoesNotThrow(123)

const return99onFailure = catchWrap(functionThatMightThrow, 99)
const resultOr99 = return99onFailure(234)

Properly supports types for the returned function if the function being wrapped has up to 10 overload signatures, adding the type of the caughtValue (or undefined if not provided) to the return type of each signature. For example:

function x(): boolean
function x(s: string): string
function x(s?: string) {
  if (typeof s === 'undefined') return true
  else if (typeof s === 'string') return s
  else throw new Error('not a string or undefined')
}

const wrapped = catchWrap(x, 99)
const b = wrapped()
//    ^? - boolean | 99
const z = wrapped('y')
//    ^? - string | 99

See the typedocs for detailed API info.

catcher's People

Contributors

isaacs avatar damianstasik avatar

Stargazers

Derek Lewis avatar Sabin Pandey avatar  avatar Tomi Heino avatar Benedict Allerberger avatar lupu avatar Derek avatar nathfavour avatar mtt avatar  avatar Martín Acosta avatar Jeff Reiner avatar Alexandre Saraiva Moreira avatar Karol Fabjańczuk avatar Laode Muhammad Al Fatih avatar Florian Lepage avatar Zhazha_JiaYiZhen avatar  avatar Joscha Götzer avatar 國舛等志 avatar  avatar Kainoa Kanter avatar Pacotole avatar Joe Mordica avatar Thiha avatar Vlad Sazonau avatar Abdur-Rahman avatar Martin Wienc avatar Steven R. Loomis avatar Michael Demarais avatar Tero Jaakkola avatar Valentin Semirulnik avatar Janak Nirmal avatar  avatar Nabeel Valley avatar Juho Vepsäläinen avatar  avatar Geoff Goodman avatar Ehsan A avatar Sandalots avatar Steve McNiven-Scott avatar Esteban De la Rosa avatar Vance Lucas avatar Christian Owusu avatar Dimas Yudha Pratama avatar AlexR avatar Fernando Daciuk avatar Konstantin Vyatkin avatar Greg Huber avatar Thawan Keane avatar Jeeho Ahn avatar Oliver Bell avatar Ruy Adorno avatar Kayode Ogunmakinwa avatar Kelvin Yong avatar Mitch Stewart avatar Pedro Pereira avatar Ziggy Jonsson avatar Marwan Hilmi avatar Emanuele Orfanelli avatar Abid Famasya avatar 沚水 avatar Sukka avatar Noel da Costa avatar Lark avatar Vladislav Forsh avatar Lawrence Onah avatar Léo Pradel avatar Ifeora Okechukwu avatar Daniel Abromeit aka Abro avatar omar avatar Mattèo Gauthier avatar Brandon Orther avatar Richie McColl avatar Abdulhamit Akaslan avatar  avatar Esat Saçkan avatar Marcis Bergmanis avatar Ivan Hrabčák avatar Mathieu Ledru avatar Okiki Ojo avatar TOMIKAWA Sotaro avatar Avin Lambrero avatar Andrei Luca avatar Johnie Hjelm avatar Pip Rees avatar calloc134 avatar sen ✦ avatar Kirill Groshkov avatar Kristóf Poduszló avatar pongstr avatar Siddharth avatar Truong Hoang Dung avatar Altay avatar Nathan Knowler avatar ΝΙΚΟΛΑΣ avatar Augie avatar Eric Chernuka avatar Josephus Paye II avatar Reid Burke avatar

Watchers

 avatar  avatar

Forkers

damianstasik

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.