Skip to content
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

Typescript asynchronous decorators decorate synchronous methods #48022

Closed
GO-DIE opened this issue Feb 24, 2022 · 3 comments
Closed

Typescript asynchronous decorators decorate synchronous methods #48022

GO-DIE opened this issue Feb 24, 2022 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@GO-DIE
Copy link

GO-DIE commented Feb 24, 2022

How to correctly identify synchronous methods that have been modified to be asynchronous when using asynchronous decorators to decorate synchronous methods.

💻 Code

function AsyncDecorator() {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const originalMethod = descriptor.value;
    descriptor.value = async function (...args: any[]) {
      await sleep(100);
      const result = await originalMethod.apply(this, args);
      return result;
    };
    return descriptor;
  };
}

async function sleep(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

class Test {
  // This function appears to be a synchronous function, but has been changed to an asynchronous function by an asynchronous decorator, but I didn't know that
  @AsyncDecorator()
  fun(): string {
    return 'Hello World!';
  }
}

const result = new Test().fun();
console.log(result); // Expect results: Hello World!  Actual result: Promise {<pending>}

🙁 Actual behavior

Promise {<pending>}

🙂 Expected behavior

Hello World!

@MartinJohns
Copy link
Contributor

MartinJohns commented Feb 24, 2022

You need #4881, which is not supported (and likely won't be for quite a while, at least until the decorator proposal is finally finished).

@jcalz
Copy link
Contributor

jcalz commented Feb 24, 2022

Is this a bug report or a question? If it's a bug report you didn't fill out the full template; could you go back and do that?. If it's a question, then it doesn't belong here; maybe Stack Overflow is a better place, oh wait, you already opened a question there. 😃

In any case I'm not sure why your expected behavior is "Hello World!" given that you are logging a promise to the console, seemingly intentionally. I think your real expected behavior is that new Test().fun() should be seen as a Promise<string> instead of as a string and that the compiler should complain if you write, say, new Test().fun().toUppercase() instead of new Test().fun().then(x => x.toUpperCase()).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 24, 2022
@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 27, 2022

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants