README
type-async-decorator
Usage
For using async func by decorator.
function AsyncMethodDecorator(
beforeFunc?: (() => Promise<void>) | (() => void) | undefined,
afterFunc?: (() => Promise<void>) | (() => void) | undefined,
catchFunc?: ((error: any) => Promise<void>) | (() => void) | undefined,
finalFunc?: (() => Promise<void>) | (() => void) | undefined,
): MethodDecorator;
Samples
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const delayLog = async (str: string, ms: number) => {
await delay(ms);
console.log(str);
};
@AsyncMethodDecorator(
async () => delayLog('before', 4000),
async () => delayLog('after', 2000),
async () => delayLog('failed', 1000),
async () => delayLog('fainaly', 1000),
)
async function hello() {
console.log('Hello.');
}
await hello();
// before
// Hello.
// after
// fainaly
@AsyncMethodDecorator(
async () => delayLog('before', 4000),
async () => delayLog('after', 2000),
async () => delayLog('failed', 1000),
async () => delayLog('fainaly', 1000),
)
async function fail() {
console.log('Hello.');
throw new Error()
}
await fail();
// before
// Hello.
// failed
// fainaly