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_constructor_to_async
Language: TypeScript
What it does
Detectsnew Promise((resolve) => resolve(x)) (and equivalent block-bodied executors with a single resolve(...) call) and folds them into the enclosing function as async/return x. The Promise constructor wrapper disappears.
Detector pattern
The detector atsrc/analyze/detectors/typescript/promise-constructor.ts walks ts-morph NewExpression nodes with Promise as the callee, then inspects the executor arrow function body for a single resolve-only path.
Preconditions
- The executor’s resolve path is synchronous — no
setTimeout,setInterval,process.nextTick,queueMicrotask,addEventListener, or other async-escape mechanism (preconditionno-async-escape). - Single resolve / reject path — the executor calls
resolve(orreject) at most once, and not from inside multiple branches (preconditionsingle-resolve). - No
try/catchstraddling the resolve call. - The enclosing function returns the
new Promise(...)directly.
Before / after
Edge cases handled
- Arrow-body executor (
(resolve) => resolve(x)) and block-body single-statement executor ((resolve) => { resolve(x); }). - Preserves the function name, parameters, and parameter type annotations.
- Promotes the function declaration to
async.
Edge cases NOT handled (skip via precondition)
- Executor uses
setTimeout/ event listeners to defer resolve (preconditionno-async-escape). ThedelayedValuefixture is intentionally an example of this —new Promise((resolve) => setTimeout(() => resolve(value), ms))is left alone because folding it would lose the delay semantics. - Multiple
resolve(...)calls across branches (preconditionsingle-resolve). - Executor calls
reject(...)— the rewrite would need to throw, and barethrowinside an async function changes the rejection semantics in ways that may surprise downstream.catchhandlers. new Promise((resolve, reject) => ...)where both are used.