better-async-await.macro 🎣
Write better async await and avoid the try catch statementInstallation with CRA < 2.0
npm install --save-dev babel-plugin-macros better-async-await.macro
or
yarn add babel-plugin-macros better-async-await.macro --dev
.babelrc
Installation with CRA >= 2.0
npm install --save-dev better-async-await.macro
or
yarn add better-async-await.macro --dev
⭐ Usage
; { const err resp = await ; iferr ; // else do something with the response}
Demo
Motivation and Idea
This babel macro is to use this plugin babel-plugin-better-async-await with CRA or any app which in any way relies on @babel/env or on the order of plugins or presets.
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 ; catcherr // if error show a generic error message return ; // wait for getting next set of questions api try response = await ; catcherr // if error show a generic error message return ; // finally submit application try response = await ; catcherr // if error show a generic error message return ; }
Approach with this macro and 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 = await ; if err return ;}
⚡️ The problem solved
Using this babel macro you could write async await in the alternate approach mentioned above. We will transform your async await code so that it works the
[err, resp]
way.
📒 Examples of using it in your code
Before
{ let resp; try resp = await api; catcherr ; }}
After
{ const err resp = await ; iferr ; // else do something with the response}
Before
{ let resp; try resp = await getData; catcherr ; }} { return { ; };}
After
{ const err resp = await ; iferr ; // else do something with the response} { return { ; };}
Before
{ let resp; try resp = await ; catcherr ; }}
After
{ const err resp = await ; iferr ; // else do something with the response}
📒 Babel Tranformation
In
{ const err resp = await ;}
Out
{ const err resp = await }
👍 Contribute
Show your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !
📝 License
MIT © viveknayyar
👷 TODO
- Complete README
- Add Examples and Demo
- Test Suite