1.0.2 • Published 11 months ago

@lachero/safe-try v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

safe-try

safe-try is a lightweight utility library for JavaScript that simplifies error handling by providing easy-to-use wrappers for both synchronous and asynchronous operations, returning a tuple of the result and error.

Usage

import { safeAsync, safeSync } from '@lachero/safe-try';

// async example
const [result, err] = await safeAsync(mightFailAsync());
if (!err) {
	// handle error
	return;
}

result;
// ^? 'success'

// sync example
const [result, err] = safeSync(() => mightFailSync());
if (!err) {
	// handle error
	return;
}

result;
// ^? 'success'

async function mightFailAsync() {
	if (Math.random() > 0.5) throw new Error('Failed');

	return 'success';
}

function mightFailSync() {
	if (Math.random() > 0.5) throw new Error('Failed');

	return 'success';
}

Inspired by Go Error Handling

f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with the open *File f

Inspired by ECMAScript Safe Assignment Operator Proposal

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago