Skip to content

Bug: "Convert to async function" refactor generates invalid code with a function returning Promise<function> #40150

@sapphi-red

Description

@sapphi-red

TypeScript Version: 3.9.2, 4.0.0-beta, 4.1.0-dev.20200820

Search Terms:
Convert to async function, quick fix, 80006, This may be converted to an async function

Code

const wait = (delay: number): Promise<(msg: string) => void> =>
    new Promise(resolve =>
        setTimeout(() => {
            const f = (msg: string) => console.log(msg)
            resolve(f)
        }, delay)
    )

const f = () => {
    return wait(1000).then(res => res('hello'))
}

Expected behavior:
Running 'Convert to async function' on f should generate code like below.

const wait = (delay: number): Promise<(msg: string) => void> =>
    new Promise(resolve =>
        setTimeout(() => {
            const f = (msg: string) => console.log(msg)
            resolve(f)
        }, delay)
    )

const f = async () => {
    const res = await wait(1000)
    return res('hello')
}

Actual behavior:
Running 'Convert to async function' on f generates code like below.

const wait = (delay: number): Promise<(msg: string) => void> =>
    new Promise(resolve =>
        setTimeout(() => {
            const f = (msg: string) => console.log(msg)
            resolve(f)
        }, delay)
    )

const f = async () => {
    const msg=await wait(1000)
return res('hello')
}

Playground Link: https://www.typescriptlang.org/play?#code/MYewdgzgLgBA7gQwJawLwwBQBMCmAbBATwC4YwBXAWwCMcAnASlIAU6RKkIcAeDSiAOalodJGAEMYqAHwwAbiCRZZMgFAwNZHHBit2nHBjo4IIPHJxTp6zba5QAKkko4Q5KBgySZMAN43bQJhQSFgAMylMfiEYETEJK2DwUzwcADo8EAE+QQYAoM1jFIsMMLyCmABfABoYXAJCcs081RDoGAj0L0T-W2MocjoweGQPAEYABimGNKgACxwwIxNEoowAcgW8TPWGPMrVIA

Related Issues:
with imported functions #39858

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: LS: Refactoringse.g. extract to constant or function, rename symbol

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions