type-async-decorator

[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE) [![Build Status](https://travis-ci.org/tktcorporation/type-async-decorator.svg?branch=master)](https://travis-ci.org/tktcorporation/type-async-decorator)

Usage no npm install needed!

<script type="module">
  import typeAsyncDecorator from 'https://cdn.skypack.dev/type-async-decorator';
</script>

README

type-async-decorator

MIT License Build Status

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