This repository contains a codemod script for use with JSCodeshift that help add try catch statements to asyc await calls.
yarn global add jscodeshift
git clone https://github.com/vivek12345/async-await-codemod.git
- Run
yarn install
in the async-await-codemod directory jscodeshift -t <codemod-script> <path>
codemod-script
- path to the transform file, see available scripts below;path
- files or directory to transform;- use the
-d
option for a dry-run and use-p
to print the output for comparison; - use the
--extensions
option if your files have different extensions than.js
(for example,--extensions js,jsx
); - see all available jscodeshift options.
Converts async await functions like below:-
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
response = await getSessionStatusApi();
// wait for getting next set of questions api
response = await getNextQuestionsApi();
// finally submit application
response = await submitApplication();
}
To the following with try catch statements:-
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
try {
response = await getSessionStatusApi();
} catch(e) {
console.log(e);
}
// wait for getting next set of questions api
try {
response = await getNextQuestionsApi();
} catch(e) {
console.log(e);
}
// finally submit application
try {
response = await submitApplication();
} catch(e) {
console.log(e);
}
}
jscodeshift -t transforms/async-await-with-try-catch.js <path>
If you want to replace the default console.log(e)
with your custom function call for example error.handleError(e)
,
you can pass that as an option --catchBlock
jscodeshift -t transforms/async-await-with-try-catch.js <path> --catchBlock="error.handleError(e)"
This will transform your code to:-
async function completeApplicationFlow() {
// wait for get session status api to check the status
let response;
try {
response = await getSessionStatusApi();
} catch(e) {
error.handleError(e)
}
// wait for getting next set of questions api
try {
response = await getNextQuestionsApi();
} catch(e) {
error.handleError(e)
}
// finally submit application
try {
response = await submitApplication();
} catch(e) {
error.handleError(e)
}
}
Options to recast's printer can be provided
through the printOptions
command line argument
jscodeshift -t transform.js <path> --printOptions='{"quote":"double"}'
Show your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !
MIT © viveknayyar