Await Handler

A simple package to help ease the pain of catching errors when using async/await. This provides error handling similar to Golang.

Sometimes wrapping all of your await blocks in a try/catch is not elegant. This micro-package provides a different method of handling errors from your await calls without wrapping it within a try/catch.

Let's take the following example:

module.exports = async (url) => {
  try {
    const body = await getPageSource(url);
    const html = await parseHTML(body);
    for(const image of html.images) {
      await downloadImage(image.url);

    return `Downloaded all images from ${url}`;
  } catch(err) {
    // Unless you write each one of the methods above in their own try/catch
    // you will not know where `err` came from here.
    throw err;

Since you have one try/catch here, if an Error is thrown from any of those 3 functions being called, you will most likely not know what caused the error. Was it not able to get the page source because the URL is invalid? Maybe the HTML is invalid? Or was one of the images broken? While there is a couple of ways to handle this in pure JavaScript, Await Handler makes it simple.

Let's recreate the same code above with Await Handler:

const on = require('await-handler');

module.exports = async (url) => {
  const [bodyErr, body] = await on(getPageSource(url));
  if(bodyErr) {
    console.error('There was an error while getting the page source.');
    throw bodyErr;

  const [parseErr, html] = await on(parseHTML(body));
  if(parseError) {
    console.error('There was an error while parsing the body.');
    throw parseError;

  for(const image of html.images) {
    const [err] = await downloadImage(image.url);
    if(err) {
      console.error(`Unable to download image ${image.url}, skipping...`);

  return `Downloaded images from ${url}`;

While the code is a bit more verbose, it's clear to see what is failing and a broken image will not prevent all other images from being downloaded.