createChain()
createChain<
In,Out>(...handlers): (input) =>Out
Creates a chain from an ordered list of handlers. Each handler can inspect the input and either:
- return a result (short-circuit)
- call
next(input)to pass to the next handler
The last handler's next throws if called, since there is
nothing after it. The last handler should always produce a result.
Type Parameters
In: In
The request/input type
Out: Out
The response/output type
Parameters
handlers: ...Handler<In, Out>[]
Ordered list of handlers (first = outermost)
Returns
A function that runs the chain
Since
2.4.0
Example
const log: `Handler<number, string>` = (n, next) => {
console.log(`processing ${n}`);
return next(n);
};
const double: `Handler<number, string>` = (n) => String(n * 2);
const chain = createChain(log, double);
chain(21); // logs "processing 21", returns "42"
Handler()<In, Out>
Handler<
In,Out> = (input,next) =>Out
A Handler receives an input and a next function.
It can either return a result directly (stopping the chain)
or call next(input) to delegate to the next handler.
The last handler in the chain receives a next that is never called -
it must produce a result on its own.
Type Parameters
In: In
The request/input type
Out: Out
The response/output type
Parameters
input: In
next: (input) => Out
Returns: Out
Since
2.4.0