Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clean-function

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clean-function

A clean type-safe replacement for using try-catch

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
5
decreased by-16.67%
Maintainers
0
Weekly downloads
 
Created
Source

clean-function

A replacement for using try-catch in JavaScript/TypeScript!

It seems like a lot of people hate try-catch just like me, so I built a better, clean, and type-safe solution.

How it works

You just wrap any function you have with the cleanFunction that you get from the package and you're good to go. Instead of using try-catch, you do it like this:

import { cleanFunction } from "clean-function";

const myFunction = cleanFunction(async (args: ANY_ARGS) => {
    // do anything you want
})

const { data, error } = await myFunction(args);

if (error === null) {
    console.log(data); // handle the data
} else {
    // handle errors
}

Installation

This package is intended to be used anywhere: the browser, Node.js, Deno, the edge, and it has no dependencies, install:

npm install clean-function

or using any other package manager you use in your project.

Usage

Using this package is so simple, you just wrap your function with cleanFunction.

For Asynchronous Functions

import { cleanFunction } from "clean-function";

const sum = cleanFunction(async (a: number, b: number) => {
    // do anything you want here
    return a + b;
});

const { data, error } = await sum(1, 2);

if (error === null) {
    console.log(data); // number (3)
} else {
    // handle errors as you want
    console.error(error);
}

For Synchronous Functions

import { cleanFunctionSync } from "clean-function";

const mySyncFunction = cleanFunctionSync((a: number, b: number) => {
    return a + b;
});

const { data, error } = mySyncFunction(1, 2);

if (error === null) {
    console.log(data); // number (3)
} else {
    // handle errors as you want
    console.error(error);
}

Made with ❤️ by Kais Radwan

FAQs

Package last updated on 08 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc