npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-preset-better-async-await

v1.0.0

Published

preset for babel-plugin-better-async-await

Downloads

3

Readme

babel-preset-better-async-await

All Contributors

Babel preset for the babel-plugin-better-async-await plugin. babel-plugin-better-async-await.

Install

$ npm install --save-dev babel-preset-better-async-await

or with yarn

$ yarn add babel-preset-better-async-await --dev

⭐ Usage

Via .babelrc (Recommended) without options

.babelrc

{
  "presets": ["better-async-await"]
}

If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter

{
  "presets": ["better-async-await", "@babel/env"]
}

Via .babelrc (Recommended) with options

.babelrc

{
  "presets": [
    [
      "better-async-await",
      {
        "mode": "strict"
      }
    ]
  ]
}

If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter

{
  "presets": [
    [
      "better-async-await",
      {
        "mode": "strict"
      }
    ],
    ["@babel/env"]
  ]
}

Via CLI

babel --presets better-async-await script.js

If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter

babel --presets better-async-await @babel/env script.js

Via Node API

without options:

require("babel-core").transform("code", {
  presets: ["better-async-await"]
});

with option:

require("babel-core").transform("code", {
  presets: [
    [
      "better-async-await",
      {
        mode: "strict"
      }
    ]
  ]
});

If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter

without options:

require("babel-core").transform("code", {
  presets: ["better-async-await", "@babel/env"]
});

with option:

require("babel-core").transform("code", {
  presets: [
    [
      "better-async-await",
      {
        mode: "strict"
      }
    ],
    ["@babel/env"]
  ]
});

Options

mode

  • strict: In this mode variable names on the left of await statement should match the following rule:-
const [err, resp] = await api.getData(5);
// ...

In Strict Mode

  • Variable name on the left for error should be err
  • Variable name on the left for response should be resp

In non-strict mode

  • Variable names on the left can be anything

Motivation and Idea

This babel plugin is inspired from the idea of this post https://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ written by - Dima Grossman

In async/await functions we often use try/catch blocks to catch errors.

For example:-

async function completeApplicationFlow() {
  // wait for get session status api to check the status
  let response;
  try {
    response = await getSessionStatusApi();
  } catch (err) {
    // if error show a generic error message
    return handleError(err);
  }

  // wait for getting next set of questions api
  try {
    response = await getNextQuestionsApi();
  } catch (err) {
    // if error show a generic error message
    return handleError(err);
  }

  // finally submit application
  try {
    response = await submitApplication();
  } catch (err) {
    // if error show a generic error message
    return handleError(err);
  }
}

Approach inspired from the blog and a different way of doing this could be:-

async function completeApplicationFlow() {
  // wait for get session status api to check the status
  let err, response;
  // wait for get session status api to check the status
  [err, response] = await getSessionStatusApi();
  // if error show a generic error message
  if (err) return handleError(err);
  // call getNextQuestion Api
  [err, response] = await getNextQuestionsApi();
  // if error show a generic error message
  if (err) return handleError(err);
  // finally submit application
  [err, response] = this.submitApplication();
  if (err) return handleError(err);
}

📒 Examples of using it in your code

Before

async function test() {
  let resp;
  try {
    resp = await api.getData(5);
  } catch(err)
    handleError();
  }
}

After

async function test() {
  const [err, resp] = await api.getData(5);
  if (err) handleError();
  // else do something with the response
}

Before

async function test() {
  let resp;
  try {
    resp = await getData;
  } catch(err)
    handleError();
  }
}

function getData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, 1000);
  });
}

After

async function test() {
  const [err, resp] = await getData;
  if (err) handleError();
  // else do something with the response
}

function getData() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, 1000);
  });
}

Before

async function test() {
  let resp;
  try {
    resp = await fetch('http://some-rest-endpoint');
  } catch(err)
    handleError();
  }
}

After

async function test() {
  const [err, resp] = await fetch("http://some-rest-endpoint");
  if (err) handleError();
  // else do something with the response
}

📒 Babel Tranformation

In

async function test() {
  const [err, resp] = await fetch("http://some-rest-endpoint");
}

Out

async function test() {
  const [err, resp] = await fetch("http://some-rest-endpoint")
    .then(resp => {
      return [null, resp];
    })
    .catch(error => {
      return [error];
    });
}

Contributors

Thanks goes to these wonderful people (emoji key):

| Vivek Nayyar💬 🐛 💻 🎨 📖 💡 🤔 📦 🔌 👀 | | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!