Skip to content

Instantly share code, notes, and snippets.

@Sangrene
Created March 21, 2019 15:25
Show Gist options
  • Save Sangrene/f3e89ced8a5b790eadcc75bd60271d35 to your computer and use it in GitHub Desktop.
Save Sangrene/f3e89ced8a5b790eadcc75bd60271d35 to your computer and use it in GitHub Desktop.
Decorator to log async method
export const logAction = (name: string) => {
return (target, key, descriptor) => {
if(descriptor === undefined){
descriptor = Object.getOwnPropertyDescriptor(target, key);
}
const originalMethod: Function = descriptor.value;
descriptor.value = async function() {
const args: Array<any> = [];
for(let i = 0, iMax = arguments.length; i < iMax; i++){
args[i] = arguments[i];
}
const result = await originalMethod.apply(this, args);
console.log(`Call ${name}(${JSON.stringify(args)}) returns : ${JSON.stringify(result)}`);
return result;
}
return descriptor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment