├── .gitignore ├── examples ├── nodejs │ ├── package.json │ ├── index.js │ └── README.md └── webdriverio │ ├── package.json │ ├── README.md │ └── test.js ├── package.json ├── index.js ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /examples/nodejs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "start": "babel-node index.js --plugins auto-await" 4 | }, 5 | "devDependencies": { 6 | "axios": "^0.17.0", 7 | "babel-cli": "^6.26.0", 8 | "babel-plugin-auto-await": "^0.1.0" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /examples/webdriverio/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "test": "mocha --require babel-register test.js" 4 | }, 5 | "babel": { 6 | "plugins": [ 7 | "auto-await" 8 | ] 9 | }, 10 | "devDependencies": { 11 | "babel-core": "^6.26.0", 12 | "babel-plugin-auto-await": "^0.1.0", 13 | "chai": "^4.1.2", 14 | "mocha": "^4.0.1", 15 | "webdriverio": "^4.8.0" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /examples/nodejs/index.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios'); 2 | 3 | async function loadAndParseData() { 4 | // Because this funciton is marked as async we automatically await all promises 5 | 6 | const { data: user } = axios.get('https://api.github.com/users/ziolko'); 7 | const { data: repos } = axios.get(user.repos_url); 8 | 9 | return repos.map(repo => repo.name); 10 | } 11 | 12 | loadAndParseData().then(console.log); 13 | -------------------------------------------------------------------------------- /examples/webdriverio/README.md: -------------------------------------------------------------------------------- 1 | # WebdriverIO example 2 | 3 | Using `babel-plugin-auto-await` you can write tests as if they were synchronous: 4 | ```javascript 5 | it('Should return "Google" when asked about page title', async function () { 6 | browser.url('http://www.google.com') 7 | assert.equal('Google', browser.getTitle()) 8 | }) 9 | ``` 10 | 11 | To do that configure babel to use the plugin: 12 | ```json 13 | "babel": { 14 | "plugins": [ 15 | "auto-await" 16 | ] 17 | } 18 | ``` 19 | 20 | Then simply run mocha with babel: `mocha --require babel-register test.js` -------------------------------------------------------------------------------- /examples/nodejs/README.md: -------------------------------------------------------------------------------- 1 | # nodejs example 2 | 3 | Using `babel-plugin-auto-await` you can write nodejs scripts as if they were synchronous: 4 | ```javascript 5 | async function loadAndParseData () { 6 | // Because this funciton is marked as async we automatically await all promises 7 | 8 | const { data: user } = axios.get('https://api.github.com/users/ziolko') 9 | const { data: repos } = axios.get(user.repos_url) 10 | 11 | return repos.map(repo => repo.name) 12 | } 13 | ``` 14 | 15 | To do that just use `babel-node` instead of `node` command: `babel-node index.js --plugins auto-await` -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "babel-plugin-auto-await", 3 | "version": "0.4.0", 4 | "description": "Automatically await every method call in async function", 5 | "main": "index.js", 6 | "keywords": [ 7 | "async", 8 | "await", 9 | "test", 10 | "promise", 11 | "babel", 12 | "automatic", 13 | "synchronous", 14 | "asynchronous" 15 | ], 16 | "homepage": "https://github.com/ziolko/babel-plugin-auto-await", 17 | "bugs": { 18 | "url": "https://github.com/ziolko/babel-plugin-auto-await" 19 | }, 20 | "author": "Mateusz Zieliński (https://github.com/ziolko)", 21 | "license": "MIT" 22 | } 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (babel) { 2 | var asyncFunctionVisitor = { 3 | CallExpression: function (path) { 4 | if (path.parent.type !== 'AwaitExpression') { 5 | path.replaceWith(babel.types.awaitExpression(path.node)) 6 | } 7 | }, 8 | TaggedTemplateExpression: function (path) { 9 | if (path.parent.type !== 'AwaitExpression') { 10 | path.replaceWith(babel.types.awaitExpression(path.node)) 11 | } 12 | }, 13 | Function: function (path) { 14 | path.skip() 15 | } 16 | }; 17 | 18 | return { 19 | visitor: { 20 | Function: function (path) { 21 | if (path.node.async) { 22 | path.traverse(asyncFunctionVisitor) 23 | } 24 | } 25 | } 26 | } 27 | }; -------------------------------------------------------------------------------- /examples/webdriverio/test.js: -------------------------------------------------------------------------------- 1 | const webdriverio = require('webdriverio'); 2 | const { assert } = require('chai'); 3 | 4 | const options = { desiredCapabilities: { browserName: 'chrome' } }; 5 | const browser = webdriverio.remote(options); 6 | 7 | describe('Google web search engine', function () { 8 | this.timeout(60000); 9 | 10 | // Every code using the 'browser' object has to be wrapped by wdio.wrap 11 | before(async function () { 12 | browser.init() 13 | }); 14 | 15 | after(async function () { 16 | browser.end() 17 | }); 18 | 19 | it('Should return "Google" when asked about page title', async function () { 20 | // Because this funciton is marked as async we automatically await all promises 21 | browser.url('http://www.google.com'); 22 | assert.equal('Google', browser.getTitle()) 23 | }) 24 | }); 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Mateusz Zieliński 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Write JS code as if it was synchronous 2 | 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. 3 | 4 | ## Example 5 | ```javascript 6 | const axios = require('axios') 7 | 8 | async function loadAndParseData () { 9 | // Because this function is marked as async we 10 | // automatically await all promises 11 | 12 | const url = 'https://api.github.com/users/ziolko' 13 | const { data: user } = axios.get(url) 14 | const { data: repos } = axios.get(user.repos_url) 15 | 16 | // This function is not async, so it's not touched 17 | function getRepoName(repo) { 18 | return repo.nam 19 | } 20 | 21 | return repos.map(getRepoName) 22 | } 23 | 24 | loadAndParseData().then(console.log) 25 | ``` 26 | 27 | ## Use cases 28 | This plugin was valuable for me in integration tests and nodejs utility scripts. See [examples](https://github.com/ziolko/babel-plugin-auto-await/tree/master/examples) for working code. 29 | 30 | ## Installation 31 | `npm install --save-dev babel-plugin-auto-await` 32 | 33 | ## Usage 34 | ### Via .babelrc (Recommended) 35 | ###### .babelrc 36 | ```json 37 | { 38 | "plugins": ["auto-await"] 39 | } 40 | ``` 41 | 42 | ### Via CLI 43 | `babel-node --plugins auto-await script.js` 44 | 45 | ### Via Node API 46 | ```javascript 47 | require("babel-core").transform("code", { 48 | plugins: ["auto-await"] 49 | }); 50 | ``` 51 | 52 | ## Gotchas 53 | If you don't understand how `Promise`, `async` and `await` work read [this tutorial](https://medium.com/@reasoncode/javascript-es8-introducing-async-await-functions-7a471ec7de8a) first. 54 | 55 | # License 56 | https://opensource.org/licenses/MIT 57 | --------------------------------------------------------------------------------