How to correctly identify synchronous methods that have been modified to be asynchronous when using asynchronous decorators to decorate synchronous methods.
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>}