better-try-catch
Try-catch wrapper for easy error handling. Inspired by await-to-js.
Installation
$ npm install better-try-catch
Usage
var btc = require('better-try-catch')
var [err, result] = btc(someFunction)(arg1, arg2)
if (!err) {
}
async function example1() {
var [err, result] = await btc(someFunctionThatReturnsPromise)(arg1, arg2)
if (!err) {
}
}
async function example2() {
var [err, result] = await btc.promisify(fs.readFile)('data.json', 'utf8')
}
I didn't see it's "better?"
Consider a situation like this:
try {
doSomething()
} catch (err) {
}
if (!err) {
}
Humm, it should be not too hard to solve. After one or three minutes of thinking, we have the code rearranged to be like this:
var err
try {
doSomething()
} catch (ex) {
err = ex
}
if (!err) {
}
Okay, seems the problem has gone, with the only exception that the code looks a little more messy now. C'mon! Can't we do it better? better-try-catch to the rescue™!
var btc = require('better-try-catch')
var [err, result] = btc(doSomething)()
if (!err) {
}
License
MIT License, Copyright (c) 2017 Riophae Lee