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 developmentdist
filesclean
=> runclean:dist
,clean:es
, andclean:lib
clean:dist
=> remove all existing files in thedist
folderclean:es
=> remove all existing files in thees
folderclean:lib
=> remove all existing files in thelib
folderdev
=> run webpack dev server to run example app / playgrounddist
=> runsclean:dist
andbuild
lint
=> run ESLint against all files in thesrc
folderlint:fix
=> run ESLint against all files in thesrc
folder, fixing anything it can automaticallyprepublish
=> runsprepublish:compile
when publishingprepublish:compile
=> runlint
,test:coverage
,transpile:lib
,transpile:es
, anddist
test
=> run AVA test functions withNODE_ENV=test
test:coverage
=> runtest
but withnyc
for coverage checkertest:watch
=> runtest
, but with persistent watchertranspile:lib
=> run babel against all files insrc
to create files inlib
transpile:es
=> run babel against all files insrc
to create files ines
, preserving ES2015 modules (forpkg.module
)