Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
async-fn-catch
Advanced tools
Async function error handling can be tricky. As the author of a plugin I don't want to create nearly impossible errors to catch. I like them to be exposed to the user in a simple interface.
This is a solution I have found to be useful.
npm install async-fn-catch --save
# or
yarn add async-fn-catch
// import function
const asyncCatch = require('async-fn-catch')
// or
import asyncCatch from 'async-fn-catch'
This first example shows how you wrap a function with async-fn-catch
to make the error super easy to catch. You could do the same by adding a .catch
to the foo()
call. Which is essentially what this lib is doing.
const errorHandler = err => console.log(err) // just log error
const foo = asyncCatch(
async function fooAsync () {
await bar() // error happens in here
},
errorHandler
)
async function bar () {
... // do complex stuff
throw new Error('qux')
}
foo() // logs -> Error "qux"
This next example shows how error handling with async can get a bit trickier when dealing with methods in a class.
class Foo extends EventEmitter {
constructor () {
super()
this.bar = asyncCatch(
this.bar.bind(this),
this.onError.bind(this)
)
}
onError (err) { this.emit('error', err)}
async bar () { await this.baz() }
async baz () { throw new Error('qux') }
}
const foo = new Foo()
foo.on('error', (err) => console.log(err))
foo.bar() // logs -> Error "qux"
FAQs
async function error catching can be a bit tricky
The npm package async-fn-catch receives a total of 0 weekly downloads. As such, async-fn-catch popularity was classified as not popular.
We found that async-fn-catch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.