docs: Update AsyncHandler documentation.

This commit is contained in:
Ruben Verborgh 2021-09-09 08:11:47 +01:00
parent a75d5aa63c
commit c9d09abebc

View File

@ -3,9 +3,9 @@
*/ */
export abstract class AsyncHandler<TIn = void, TOut = void> { export abstract class AsyncHandler<TIn = void, TOut = void> {
/** /**
* Checks if the input data can be handled by this class. * Checks if the input can be handled by this class.
* Throws an error if it can't handle the data. * If it cannot handle the input, rejects with an error explaining why.
* @param input - Input data that could potentially be handled. * @param input - Input that could potentially be handled.
* *
* @returns A promise resolving if the input can be handled, rejecting with an Error if not. * @returns A promise resolving if the input can be handled, rejecting with an Error if not.
*/ */
@ -15,26 +15,24 @@ export abstract class AsyncHandler<TIn = void, TOut = void> {
} }
/** /**
* Handles the given input. This should only be done if the {@link canHandle} function returned `true`. * Handles the given input. This may only be called if {@link canHandle} did not reject.
* Therefore, consider using the {@link handleSafe} function instead. * When unconditionally calling both in sequence, consider {@link handleSafe} instead.
* @param input - Input data that needs to be handled. * @param input - Input that needs to be handled.
* *
* @returns A promise resolving when the handling is finished. Return value depends on the given type. * @returns A promise resolving when handling is finished.
*/ */
public abstract handle(input: TIn): Promise<TOut>; public abstract handle(input: TIn): Promise<TOut>;
/** /**
* Helper function that first runs the canHandle function followed by the handle function. * Helper function that first runs {@link canHandle} followed by {@link handle}.
* Throws the error of the {@link canHandle} function if the data can't be handled, * Throws the error of {@link canHandle} if the data cannot be handled,
* or returns the result of the {@link handle} function otherwise. * or returns the result of {@link handle} otherwise.
* @param input - Input data that will be handled if it can be handled. * @param input - Input data that will be handled if it can be handled.
* *
* @returns A promise resolving if the input can be handled, rejecting with an Error if not. * @returns A promise resolving if the input can be handled, rejecting with an Error if not.
* Return value depends on the given type.
*/ */
public async handleSafe(input: TIn): Promise<TOut> { public async handleSafe(input: TIn): Promise<TOut> {
await this.canHandle(input); await this.canHandle(input);
return this.handle(input); return this.handle(input);
} }
} }