Skip to content

"Convert to async function" produces incorrect code for .catch() #35192

Closed
@plaa

Description

@plaa

TypeScript Version: 3.7.2 (refactoring in VSCode 1.40.1 using 3.6.3)

Search Terms: Convert to async function catch

Code

// Original function
function combine1(a: Promise<any>, b: Promise<any>) {
  return a.catch(() => b)
}

// Result of "Convert to async function" in VSCode
async function combine2(a: Promise<any>, b: Promise<any>) {
  try {
    return a;
  }
  catch (e) {
    return await b;
  }
}

async function test() {
  const a1 = Promise.reject('nay1')
  const b1 = Promise.resolve('yay1')
  console.log("combine1:", await combine1(a1, b1))
  const a2 = Promise.reject('nay2')
  const b2 = Promise.resolve('yay2')
  console.log("combine2:", await combine2(a2, b2))
}
test()

Expected behavior:

Converted code should be functionally equivalent to original code. I believe return await a would be correct.

Actual behavior:

Converted code is not equivalent as the example shows.

Playground Link:

https://www.typescriptlang.org/play/#code/PTAEHkCcEsHNoHYEMA2oBmBXBBjALtAPYICwAUFrgcaDoQLYBGiApgIwAUSAXKAAqQG0AM4sAPEgQBPAHwAaUI14ChoidJkBKUAG9yoUJBZ5MkBKCQA6HEjw4AFhw7aAvDMWbyAX3LkQoACUWYUwUPFBCdFAAIgBhYgA3FkhwvEILYSlcDGx8IgRo0ERQADUAZXiAExZyJEzsyjyaOiZWACYuZUF6EXFJWQUlfm7e9VltPTIDPEgpXX0DQ2NTcyQAbgWfKdpbB1AOFgmFgyMTMwsAdyRocMYN7a2t2vqcHKp80DxgvGd57boEMJwkg2KAXMNVCxLEYAFYsfAcADkyCkbERnn+xCBilB4JUPVE0OChBQSSRUiQqPRCwBwhJUJQhFgHGiLWYCHY3GiCiQVxutAY7PYXDYgzYmgxBlpwLaYIhBKhsPhP2RlLa1MxgNusrxI0JRjppJY5LVGqlWPplkZzNZgvaXJ5fPCbPaXDagzaEu85C+QOc5CAA

Related Issues:

Related to async refactoring: #27641, #27621

VSCode info:

Version: 1.40.1
Commit: 8795a9889db74563ddd43eb0a897a2384129a619
Date: 2019-11-13T16:47:44.719Z
Electron: 6.1.2
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Darwin x64 18.7.0

Sorry if this should be reported on VSCode, I saw similar bugs have been transferred from there to here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: 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