Giter VIP home page Giter VIP logo

p-safe's Introduction

p-safe

npm npm bundle size

Safely handle promise rejections

Useful for codes that are way out of your league and you want to handle everything! (e.g. API calls)

Install

npm install p-safe

Usage

import { trySafe } from 'p-safe';

const { error } = trySafe<void, CustomError>(async (_, reject) => {
  await promiseThatMightReject();
  const { statusText } = await fetch('...');
  if (statusText !== 'OK') {
    reject(new CustomError('Request failed for a silly reason'));
  }
});

// Return type is `SafeReturn<void, CustomError>`

if (error) {
  // Handle error
  return console.error(error);
}

console.log(error); // undefined

Or implement your function and only use SafeReturn type:

import type { SafeReturn } from 'p-safe';

async function foo(): Promise<SafeReturn<object, Error>> {
  const resp = await fetch('...');
  if (!resp.ok) {
    return { error: new Error('Request failed for a silly reason') };
  }
  return { data: await resp.json() };
}

const { data, error } = await foo();

if (error) {
  console.log(error); // ERROR
  console.log(data); // undefined
  return;
}

console.log(error); // undefined
console.log(data); // { ... }

Are you still confused? Let me explain it in a simple way. p-safe is a simple utility that helps you to safely handle promise rejections. It's a simple and clean way to handle promise rejections without using try/catch blocks.

Check out the tests for more examples.

Related

  • p-catch-if - Conditional promise catch handler
  • p-if - Conditional promise chains
  • p-tap - Tap into a promise chain without affecting its value or state
  • p-log - Log the value/error of a promise
  • More…

License

MIT © Shahrad Elahi

p-safe's People

Contributors

shahradelahi avatar

Stargazers

 avatar Luiz Guilherme avatar  avatar Yefferson Silva avatar  avatar Abdulkareem seefUldeen avatar Aldous Palen avatar Iman avatar  avatar  avatar  avatar Josué Diogo Pedro avatar rdcyosuzume avatar Zaixu Zheng avatar Dmitry Tatarintsev avatar The Strongest Algorithm avatar Délcio Capolo avatar yamataku0518  avatar alireza javadi avatar Abdalaziz (AOT) avatar Cryptopapas avatar 1st Developer  avatar Legato Bluesummers avatar  avatar

Watchers

 avatar  avatar

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.