Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sapphi-red opened this issue Aug 20, 2020 · 0 comments · Fixed by #40191
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol
Milestone

Comments

@sapphi-red
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol
Projects
None yet
2 participants