babel-preset-better-async-await
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
.babelrc
(Recommended) without options
Via .babelrc
If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter
.babelrc
(Recommended) with options
Via .babelrc
If you are using babel-preset-env or @babel/env or babel-plugin-transform-async-to-generator, then the order of presets matter
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:
;
with option:
;
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:
;
with option:
;
Options
mode
strict
: In this mode variable names on the left of await statement should match the following rule:-
const err resp = await api;// ...
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:-
{ // wait for get session status api to check the status let response; try response = await ; catch err // if error show a generic error message return ; // wait for getting next set of questions api try response = await ; catch err // if error show a generic error message return ; // finally submit application try response = await ; catch err // if error show a generic error message return ; }
Approach inspired from the blog and a different way of doing this could be:-
{ // 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 ; // if error show a generic error message if err return ; // call getNextQuestion Api err response = await ; // if error show a generic error message if err return ; // finally submit application err response = this; if err return ;}
📒 Examples of using it in your code
Before
{ let resp; try resp = await api; catcherr ; }}
After
{ const err resp = await api; if err ; // else do something with the response}
Before
{ let resp; try resp = await getData; catcherr ; }} { return { ; };}
After
{ const err resp = await getData; if err ; // else do something with the response} { return { ; };}
Before
{ let resp; try resp = await ; catcherr ; }}
After
{ const err resp = await ; if err ; // else do something with the response}
📒 Babel Tranformation
In
{ const err resp = await ;}
Out
{ const err resp = await ;}
Contributors
Thanks goes to these wonderful people (emoji key):
Vivek Nayyar 💬 🐛 💻 🎨 📖 💡 🤔 📦 🔌 👀 |
---|
This project follows the all-contributors specification. Contributions of any kind welcome!