Skip to content

convertToAsyncFunction: Fix two-argument then#37588

Closed
andrewbranch wants to merge 3 commits intomicrosoft:masterfrom
andrewbranch:bug/27621
Closed

convertToAsyncFunction: Fix two-argument then#37588
andrewbranch wants to merge 3 commits intomicrosoft:masterfrom
andrewbranch:bug/27621

Conversation

@andrewbranch
Copy link
Member

Fixes #27621 - a two-argument then is supported only if no other .then follows it in the chain.

@sandersn sandersn added the For Milestone Bug PRs that fix a bug with a specific milestone label Apr 15, 2020
Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't dig into the reasons this change was needed, but I agree that the code change has the behavior you claim.

const transformationBody = getTransformationBody(onFulfilled, prevArgName, onFulfilledArgumentName, node, transformer);

if (onRejected) {
// From:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. I wish more of our transformations had comments like these.

function ensureReturnOrThrow(statements: readonly Statement[]): readonly Statement[] {
const lastStatement = lastOrUndefined(statements);
if (lastStatement?.kind !== SyntaxKind.ReturnStatement && lastStatement?.kind !== SyntaxKind.ThrowStatement) {
return statements.concat(createReturn());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this could be unreachable (e.g. if the last statement is actually an if-else in which both branches return)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, you’re right. I’m not sure there’s an efficient way to know what to do here.

const transformationBody2 = getTransformationBody(onRejected, prevArgName, onRejectedArgumentName, node, transformer);
const onFulfilledArgType = onFulfilledArgumentName && !transformer.isInJSFile && transformer.checker.getTypeAtLocation(getNode(onFulfilledArgumentName));
const onFulfilledArgTypeNode = onFulfilledArgType && !(onFulfilledArgType.flags & TypeFlags.Any) ? transformer.checker.typeToTypeNode(onFulfilledArgType) : undefined;
const onFulfilledArgVariableDeclaration = createVariableOrAssignmentOrExpressionStatement(onFulfilledArgumentName, /*rightHandSide*/ undefined, onFulfilledArgTypeNode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this name need to be unique-ified?

@andrewbranch
Copy link
Member Author

I think I’m convinced that any refactored code that comes from a two-argument then is either going to be ugly, wrong, or both. I’m going to open a new PR disabling the refactor for two-argument thens.

@andrewbranch andrewbranch deleted the bug/27621 branch April 23, 2020 23:42
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

"Convert to async function" does not handle .then(fn1, fn2) correctly

5 participants