This package has been deprecated

Author message:

This plugin is no longer needed since the bug it works around has been fixed in regenerator: https://github.com/facebook/regenerator/issues/175

babel-plugin-async-try-catch

0.0.9 • Public • Published

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 AND LICENSE

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.

Package Sidebar

Install

npm i babel-plugin-async-try-catch

Weekly Downloads

3

Version

0.0.9

License

Artistic-2.0

Last publish

Collaborators

  • chocolateboy