https://github.com/vivek12345/async-await-codemod
Codemod to add try catch to all the async await statements
https://github.com/vivek12345/async-await-codemod
async-await codemod jscodeshift recast
Last synced: 12 days ago
JSON representation
Codemod to add try catch to all the async await statements
- Host: GitHub
- URL: https://github.com/vivek12345/async-await-codemod
- Owner: vivek12345
- License: mit
- Created: 2019-01-31T10:29:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-16T23:08:36.000Z (about 6 years ago)
- Last Synced: 2025-04-28T16:03:49.220Z (12 days ago)
- Topics: async-await, codemod, jscodeshift, recast
- Language: JavaScript
- Homepage:
- Size: 95.7 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-codemods - async-await-codemod - Codemod to add try catch to all the async await statements. (ESNext)
README
## ✨ async-await-codemod [](https://travis-ci.org/vivek12345/async-await-codemod)
This repository contains a codemod script for use with
[JSCodeshift](https://github.com/facebook/jscodeshift) that help add try catch statements to asyc await calls.### 🚚 Setup & Run
1. `yarn global add jscodeshift`
1. `git clone https://github.com/vivek12345/async-await-codemod.git`
1. Run `yarn install` in the async-await-codemod directory
1. `jscodeshift -t `
* `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](https://github.com/facebook/jscodeshift#usage-cli).### 📒 Included Script
#### `async-await-with-try-catch`
Converts async await functions like below:-
```javascript
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:-
```javascript
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);
}
}```
## ⭐ Usage
```sh
jscodeshift -t transforms/async-await-with-try-catch.js
```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````sh
jscodeshift -t transforms/async-await-with-try-catch.js --catchBlock="error.handleError(e)"
```This will transform your code to:-
```javascript
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)
}
}```
### Recast Options
Options to [recast](https://github.com/benjamn/recast)'s printer can be provided
through the `printOptions` command line argument```sh
jscodeshift -t transform.js --printOptions='{"quote":"double"}'
```## 👍 Contribute
Show your ❤️ and support by giving a ⭐. Any suggestions and pull request are welcome !
### 📝 License
MIT © [viveknayyar](https://github.com/vivek12345)