I\'m trying to decorate async function#1 with some async function#2.
E.g.
function func2(param) {
return (target: any, propertyKey: string, descriptor
Decorators can only be used on a class method not on a regular function, so that is one limitation, but if you put the function within a class you can easily replace the original function and perform other async tasks:
function func2(param: number) {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(... params: any[])=> Promise<any>>) => {
let oldFunc = descriptor.value;
descriptor.value = async function (){
var result = await oldFunc.apply(this, arguments);
await delay(param) //some async operation
console.log("delay 3");
return result;
}
}
}
class Test {
@func2(1000)
async func1(timout: number) {
await delay(timout) //some async operation
console.log("delay 1");
await delay(timout) //some async operation
console.log("delay 2");
}
}
new Test().func1(1000);
// Util function
async function delay(timeout: number) {
return new Promise<void>((resolve) => setTimeout(() => {
resolve();
}, timeout));
}