How to correctly identify synchronous methods that have been modified to be asynchronous when using asynchronous decorators to decorate synchronous methods.
💻 Code
functionAsyncDecorator(){return(target: any,propertyKey: string,descriptor: PropertyDescriptor)=>{constoriginalMethod=descriptor.value;descriptor.value=asyncfunction(...args: any[]){awaitsleep(100);constresult=awaitoriginalMethod.apply(this,args);returnresult;};returndescriptor;};}asyncfunctionsleep(ms: number){returnnewPromise(resolve=>setTimeout(resolve,ms));}classTest{// 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!';}}constresult=newTest().fun();console.log(result);// Expect results: Hello World! Actual result: Promise {<pending>}
🙁 Actual behavior
Promise {<pending>}
🙂 Expected behavior
Hello World!
The text was updated successfully, but these errors were encountered:
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()).
GO-DIE commentedFeb 24, 2022
How to correctly identify synchronous methods that have been modified to be asynchronous when using asynchronous decorators to decorate synchronous methods.
Promise {<pending>}
Hello World!
The text was updated successfully, but these errors were encountered: