Skip to content

Function: After() ​

After<ClassPrototype, Method>(afterFunc): MethodDecorator<ClassPrototype, Method>

Run a function after each call of the decorated method, with its result. The function is called with the instance as this; a value it returns other than undefined replaces the result, undefined keeps it. Several @After on a method run in turn, each one receiving the result left by the previous one; an error thrown by the function is thrown by the call.

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Object

Method ​

Method extends (...args) => unknown

Parameters ​

afterFunc ​

AfterFunction<ClassPrototype, Method>

The function receiving the method's result (awaited when the method is async), and returning a value of the method's return type (a promise for an async method). It may be async only when the method is async.

Returns ​

MethodDecorator<ClassPrototype, Method>

The method decorator.

Throws ​

When the class is defined, if afterFunc is async and the method is not, or if the decorated member is not a method.

Example ​

typescript
import {Component} from 'lakutata'
import {After} from 'lakutata/decorator/asst'

class Prices extends Component {
    //The prices are rounded to cents, whatever their computation
    @After(async function (this: Prices, price: number): Promise<number> {
        return Math.round(price * 100) / 100
    })
    public async withTax(price: number): Promise<number> {
        return price * 1.196
    }
}

Released under the MIT License.