trike
Simple try/catch wrapper that runs a function and returns an error-first array
Last updated a year ago by aaronmadsen .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install trike 
SYNC missed versions from official npm registry.

Trike - try/c(atch)

Trike is a simple try/catch wrapper around a function that returns an array similar to the common node error-first callback convention. This serves two main purposes:

  1. Successful and errored calls can be easily distinguished through simple array destructuring.
  2. the try/catch is extracted from the calling and called functions, allowing for better optimization.

Installation

npm install trike

Use

const trike = require('trike');

const [err, foo] = trike(require, 'foo');

if (err) {
	console.error('Foo could not be required', err);
	return;
}

// use foo...

trike(fn[, ...args])

Trike takes a function to be called as it first argument. Any additional arguments passed to trike will be passed to this function.

const trike = require('trike');

const [err, joined] = trike((...args) => (
	args.join(' ')
), 'One', 'fish.', 'Two', 'fish.');

console.error(err); // 'null'

console.log(joined); // 'One fish. Two fish.'

async / await support

Trike now has support for async / await where the runtime natively supports it. If you pass an async function (or other function that returns a Promise) to trike() it will return a Promise that will resolve to trike's normal error-first callback-like array with the error and result set by resolving or rejecting the passed function.

// Notice the use of an immediately invoked async function
// expession just to demonstrate awaiting an async trike.
(async () => {
  const [err, result] = await trike(async () => {
    return 'Success!'
  });

  console.error(err);  // null
  console.log(result); // 'Success!'
})();

License

Copyright (c) 2018 Aaron Madsen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Current Tags

  • 1.1.3                                ...           latest (a year ago)

6 Versions

  • 1.1.3                                ...           a year ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 10
Dependencies (0)
None
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |