Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-async-try-catch

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-async-try-catch

A Babel plugin which wraps the body of async functions in a try/catch block

  • 0.0.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NOTICE: This plugin is deprecated and is no longer needed since the bug it works around has been fixed in regenerator.

babel-plugin-async-try-catch

npm status build status

A Babel plugin which wraps the body of async functions in a try/catch block

INSTALL

npm install babel-plugin-async-try-catch

USAGE

$ babel --plugins async-try-catch script.js

SYNOPSIS

$ cat before.js

function asyncError (error) {
    console.error('error:', error);
}

async function printFile (filename) {
    let contents = await fs.readFileAsync(filename, 'utf8');
    console.log(contents);
}

$ babel --plugins async-try-catch --whitelist es7.asyncFunctions before.js

function asyncError (error) {
    console.error('error:', error);
}

async function printFile (filename) {
    try {
        let contents = await fs.readFileAsync(filename, 'utf8');
        console.log(contents);
    } catch (error) {
        asyncError.call(this, error);
    }
}

DESCRIPTION

This is a Babel plugin which wraps the body of async functions in a try/catch block.

If an exception is thrown, it is passed to a callback whose this value is set to the this value inside the catch block. The callback name is currently hardwired to asyncError, but this will be configurable when Babel adds support for plugin options.

If an async function's sole top-level statement is a try/catch, try/finally or try/catch/finally statement, it is not wrapped.

Why?

The ES7 async/await proposal makes asynchronous programming in JavaScript heavenly, but async functions have one major gotcha: they silently swallow exceptions raised within the body of the function. As a result, it is recommended to wrap the body of async functions inside a try/catch block:

Another, more insidious problem is that you have to be careful to wrap your code in try/catches, or else a promise might be rejected, in which case the error is silently swallowed. (!)

My advice is to ensure that your async functions are entirely surrounded by try/catches, at least at the top level

Nolan Lawson — Taming the asynchronous beast with ES7

This plugin automatically surrounds the body of async functions with a try/catch block, so you can take advantage of the sanity and simplicity of async/await without the boilerplate.

Custom Error Handling

If you find yourself still manually writing try/catch blocks in order to perform custom error handling, it's worth remembering that the callback can be defined/overridden locally e.g. rather than writing:

async function printFile (filename) {
    try {
        let contents = await fs.readFileAsync(filename, 'utf8');
        console.log(contents);
    } catch (error) {
        console.error(`error reading ${filename}:`, error.stack);
    }
}

- you could write:

async function printFile (filename) {
    var asyncError = error => console.error(`error reading ${filename}:`, error.stack);
    let contents = await fs.readFileAsync(filename, 'utf8');
    console.log(contents);
}

Note the use of var (rather than let or const) to hoist the asyncError declaration out of the try block so that it remains visible in the catch block.

It's also worth remembering that information about the call site at which an error occurred can easily be determined from an exception's stack trace.

SEE ALSO

VERSION

0.0.9

AUTHOR

chocolateboy

Copyright © 2015 by chocolateboy

This module is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.

Keywords

FAQs

Package last updated on 27 Sep 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc