Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ziolko/babel-plugin-auto-await

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Write JS code as if it was synchronous

ES7 introduced async and await which are great. While working with them I've found that await is usually superfluous. This babel plugin automatically puts await in front of every function call in async functions. In the result the code looks like if JS was synchronous.

Example

const axios = require('axios')

async function loadAndParseData () {
  // Because this function is marked as async we
  // automatically await all promises

  const url = 'https://api.github.com/users/ziolko'
  const { data: user } = axios.get(url)
  const { data: repos } = axios.get(user.repos_url)

  // This function is not async, so it's not touched
  function getRepoName(repo) {
    return repo.nam
  }

  return repos.map(getRepoName)
}

loadAndParseData().then(console.log)

Use cases

This plugin was valuable for me in integration tests and nodejs utility scripts. See examples for working code.

Installation

npm install --save-dev babel-plugin-auto-await

Usage

Via .babelrc (Recommended)

.babelrc
{
  "plugins": ["auto-await"]
}

Via CLI

babel-node --plugins auto-await script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["auto-await"]
});

Gotchas

If you don't understand how Promise, async and await work read this tutorial first.

License

https://opensource.org/licenses/MIT

About

Automatically await every expression in async function

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.