Skip to content

codefix convertToAsync does not add await for returned values #27544

Closed
@rradczewski

Description

@rradczewski

First of all, thanks a lot for this refactoring, I think it's really useful and will push the adoption of async-await syntax 👍

This is a bug report related to this Twitter conversation: The converted code does not await for Promise.reject, thus the runtime does not throw but returns Promise.reject(3), which does not trigger the catch-clause but makes the function call return a rejected Promise.

I'm not 100% sure what the best conversion would be, but I assume it would be to always await for functions that could possibly return a Promise or expressions that are a Promise.

TypeScript Version: Tested against https://github.com/Microsoft/TypeScript/tree/v3.1.1 and 4ed85b7

Failing testcases:

These assume that the correct behavior is to add await to a returned expression that yields a Promise.

Search Terms:

  • async
  • convertToAsync

Code

function testcase() {
  return Promise.resolve(1)
    .then(x => Promise.reject(2))
    .catch(err => console.log(err));
}

Running testcase() code will log 2 and the returned Promise will resolve to undefined.

Expected behavior:

async function testcase() {
    try {
        const x = await Promise.resolve(1);
        return await Promise.reject(2);
    }
    catch (err) {
        return console.log(err);
    }
}

A call to testcase() would print 2 and the returned Promise would resolve to undefined as above.

Actual behavior:

async function testcase() {
    try {
        const x = await Promise.resolve(1);
        return Promise.reject(2);
    }
    catch (err) {
        return console.log(err);
    }
}

A call to testcase() this won't print 2 but node will complain about an unhandled rejected promise.

Related Issues:

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions