npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tcf

v2.1.0

Published

A functional try / catch / finally with async support

Downloads

3

Readme

tcf

A functional try / catch / finally with async support

Table of contents

Usage

import tcf from "tcf";

// use for inline synchronous operations
const syncResult = tcf(
  () => {
    // ... some dangerous computation

    return computed;
  },
  (error) => console.error(error),
  () => cleanup()
);

// or for asynchronous operations
const asyncResult = await tcf(
  async () => {
    // some dangerous computation

    return computed;
  },
  (error) => console.error(error),
  async () => await cleanup()
);

Available methods

tcf

tcf(tryFn: function(): any[, catchFn: function(Error): any[, finallyFn: function(): any]]): any

Also available as the default export

Run a try / catch / finally and return the result. If the result of tryFn is a Promise, then it is processed using tcfAsync, else it is processed using tcfSync. If no catchFn is passed, then tryFn is silently caught.

tcfAsync

tcfAsync(tryFn: function(): Promise[, catchFn: function(Error): any[, finallyFn: function(): any]]): Promise

Also aliased as tcf.async

Run an asynchronous try / catch / finally and return the result. This has the same contract as tcfSync, but handles Promise values returned from tryFn as well as async functions.

NOTE: This aligns with the Promise.prototype.finally specification, but also has the same contract as its synchronous counterpart, which means like in tcf any return from finallyFn will override any returns from tryFn or catchFn.

import { tcfAsync } from "tcf";

const result = await tcfAsync(async () => "foo", null, async () => "bar");

console.log(result); // bar

It is recommended that you not return anything from finallyFn to avoid this potentially unexpected behavior. (See this for more details)

tcfSync

tcfSync(tryFn: function(): any[, catchFn: function(Error): any[, finallyFn: function(): any]]): any

Also aliased as tcf.sync

Run a synchronous try / catch / finally and return the result. If no catchFn is passed, then tryFn is silently caught.

NOTE: This aligns with the specification, which means that returns from the finallyFn function will override any returns from tryFn or catchFn.

import { tcfSync } from "tcf";

const result = tcfSync(() => "foo", null, () => "bar");

console.log(result); // bar

It is recommended that you not return anything from finallyFn to avoid this potentially unexpected behavior. (See this for more details)

tf

tf(tryFn: function(): any[, finallyFn: function(): any]): any

Run a try / finally and return the result. If the result of tryFn is a Promise, then it is processed using tcfAsync, else it is processed using tcfSync.

NOTE: This is exactly the same as tcf, but without catch applied. This is usefull if you still want an error to be thrown if encountered, but you also need to do cleanup.

tfAsync

tfAsync(tryFn: function(): Promise[, finallyFn: function(): any]): Promise

Also aliased as tf.async

Run an asynchronous try / finally and return the result. This has the same contract as tfSync, but handles Promise values returned from tryFn as well as async functions.

NOTE: This is exactly the same as tcf, but without catch applied. This is usefull if you still want an error to be thrown if encountered, but you also need to do cleanup.

import { tfAsync } from "tcf";

const result = await tfAsync(async () => "foo", async () => "bar");

console.log(result); // bar

tfSync

tfSync(tryFn: function(): any[, finallyFn: function(): any]): any

Also aliased as tf.sync

Run a synchronous try / finally and return the result.

NOTE: This is exactly the same as tcf, but without catch applied. This is usefull if you still want an error to be thrown if encountered, but you also need to do cleanup.

import { tfSync } from "tcf";

const result = tfSync(() => "foo", () => "bar");

console.log(result); // bar

setResolver

setResolver(resolver: function): boolean

Sets a custom resolver of tryFn for tcfAsync. The default resolver internally uses native Promise syntax, so this function is often used when using a custom library instead.

import Bluebird from "bluebird";
import { setResolver } from "tcf";

const customResolver = tryFn => new Bluebird(resolve => resolve(tryFn()));

The default resolver also creates a new promise wrapping the one returned by tryFn, so this method can also be used to instead leverage the existing promise.

import { setResolver } from "tcf";

const customResolver = tryFn => tryFn;

Development

Standard stuff, clone the repo and npm install dependencies. The npm scripts available:

  • build => run rollup to build development dist files
  • clean => run clean:dist, clean:es, and clean:lib
  • clean:dist => remove all existing files in the dist folder
  • clean:es => remove all existing files in the es folder
  • clean:lib => remove all existing files in the lib folder
  • dev => run webpack dev server to run example app / playground
  • dist => runs clean:dist and build
  • lint => run ESLint against all files in the src folder
  • lint:fix => run ESLint against all files in the src folder, fixing anything it can automatically
  • prepublish => runs prepublish:compile when publishing
  • prepublish:compile => run lint, test:coverage, transpile:lib, transpile:es, and dist
  • test => run AVA test functions with NODE_ENV=test
  • test:coverage => run test but with nyc for coverage checker
  • test:watch => run test, but with persistent watcher
  • transpile:lib => run babel against all files in src to create files in lib
  • transpile:es => run babel against all files in src to create files in es, preserving ES2015 modules (for pkg.module)