Converting Promise Chains to Async/Await
Modern JavaScript/TypeScript codebases often need to migrate from Promise-based code to the more readable async/await syntax. Codegen provides powerful tools to automate this conversion while preserving business logic and handling complex scenarios.
You can find the complete example code in our examples repository.
Finding Promise Chains
Codegen offers multiple ways to locate Promise chains in your codebase:
- In files
- In functions
- Part of a function call chain
Promise Chains in a File
Find all Promise chains in a file:
Promise Chains in a Function
Find Promise chains within a specific function:
Promise Chain starting from a Function Call
Find Promise chains starting from a specific function call:
Converting Promise Chains
In-Place Conversion
Convert Promise chains directly in your codebase:
Handle Business Logic Without In-Place Edit
Generate the transformed code without inplace edit by returning the new code as a string. This is useful when you want to add additional business logic to the overall conversion.
Supported Promise Chain Patterns
- Basic
promise.then()
statements of any length - Catch
promise.then().catch()
statements of any length - Finally
promise.then().catch().finally()
statements of any length - Desctructure
promise.then((var1, var2))
statements ->let [var1, var2] = await statement;
- Implicit returns ->
return promise.then(() => console.log("hello"))
- Top level variable assignments ->
let assigned_var = promise.then()
- Top level variable assignments ->
let assigned_var = promise.then()
- Ambiguous/conditional return blocks
A list of all the covered cases can be found in the example notebook.
Examples
1. Basic Promise Chains
Applying the conversion…
2. Error Handling with Catch/Finally
Applying the conversion…
3. Promise.all with Destructuring
Applying the conversion…
4. Handling Ambiguous Returns Using Anonymous functions
For then
blocks that have more than one return statement, Codegen will add an anonymous function to handle the ambiguous return to guarantee a deterministic conversion.
Applying the conversion…
Handling Top-Level Assignment Variables
When converting Promise chains that involve top-level assignment variables, you can specify the variable name of your choice or pick the default which is the original variable assignment name.
Next Steps
Converting Promise chains to async/await improves code readability and maintainability. Codegen’s tools make this migration process automated and reliable, handling complex cases while preserving business logic. Here are some next steps to ensure a successful migration:
- Ensure to run
npx prettier --write .
after the migration to fix indentation + linting - Incremental Migration: Convert one module at a time
- Handle Additional Business Logic: Use
.promise_statement.edit()
to modify the entire chain and handle external business logic - If the specific conversion case is not covered, open an issue on the Codegen repository or try to right your own transformation logic using the codegen-sdk
Was this page helpful?