Transform ID:Documentation Index
Fetch the complete documentation index at: https://docs.refactron.dev/llms.txt
Use this file to discover all available pages before exploring further.
promise_chains_to_async
Language: TypeScript
What it does
Rewritesfn(...).then(a => ...).then(b => ...) chains as async functions with await and named const bindings for each intermediate value. Both flat (a().then(b).then(c)) and nested (a().then(x => b(x).then(y => y))) shapes are handled.
Detector pattern
The detector atsrc/analyze/detectors/typescript/promise-chains.ts walks ts-morph CallExpression nodes whose expression is a PropertyAccessExpression named then, climbing the chain to identify the originating call.
Preconditions
- The function body returns the chain (or the chain is the only statement). Mid-function chains used for side effects only are skipped.
- No
.catch(...)in the chain. The transform doesn’t synthesizetry/catchblocks — pre-existing error handling would change semantics. - No
Promise.all/Promise.race/Promise.allSettled/Promise.anyinside the chain — combinator semantics aren’t trivially rewritable as sequential awaits. - The enclosing function returns a Promise (so adding
asyncis type-preserving).
Before / after
Edge cases handled
- Flat chains (
a().then(b).then(c)). - Nested chains (
a().then(x => b(x).then(y => y))). - Preserves the enclosing function’s exports / signature shape.
Edge cases NOT handled (skip via precondition)
- Chain contains
.catch(...)(preconditions:no-catch). - Chain contains
Promise.all/Promise.race/Promise.allSettled/Promise.any(precondition:no-promise-combinator). - Chain handler returns void / does side effects only.
- Multiple parallel chains on the same value.