Promise -> Async/Await in Twilio Node.js SDK
Using Codegen to automate the conversion of 592 instances of Promise .then
chains to async/await
in Twilio’s Node.js Repository
Promise .then()
chains often lead to nested, hard-to-read code. While async/await
offers a cleaner alternative, migrating large codebases like Twilio’s Node.js SDK requires careful handling of backward compatibility.
Using Codegen, we performed this conversion reliably across all 592 instances.
Here is the conversion Pull Request made using Codegen.
The Pattern
Twilio’s Node.js SDK has this Promise chain pattern repeated across the codebase:
Each instance of this is found:
- Creating an
operationPromise
- Transforming the response
- Handling callbacks through
setPromiseCallback
- Returning the promise
Converting the Promise Chain to Async/Await
To perform this conversion, we used the Codegen convert_to_async_await()
api.
Learn more about the the usage of this api in the following tutorial
Step 1: Finding Promise Chains
Extract all promise chains using the TSFunction.promise_chains
method.
Step 2: Converting to Async/Await
Then we converted each chain and added proper error handling with try/catch:
The Conversion
- Converted 592 Promise chains across the codebase
- Eliminated the need for
setPromiseCallback
utility - Maintained backward compatibility with callback-style APIs
- Improved code readability and maintainability
Conclusion
Promise chains using .then()
syntax often leads to complex and deeply nested code that’s harder to maintain. It’s an active problem that many teams want to pursue but never end up doing so due to the time consuming nature of the migration.
Codegen can significantly accelerate these migrations by automating the conversion for several different cases.
Want to try this yourself? Check out our Promise to Async/Await tutorial
Was this page helpful?