Just Callback<x, a>
interface definition for use with flow projects that interface with nodejs callback based APIs. Callback<x, a>
is a function that can be used to report success or failure of the async operation:
If operation is succeeds callback must be invoked with two arguments first of type either null|void
and second of type a
- callback(null, value)
If operation is fails callback must be invoked with single argument of type x
- callback(error)
Please note that success type paramater is optional and defaults to void
there for callbacks that don't pass data back can be typed as Callback<string>
and also be invoked as callback()
to report success and callback('Oops')
to report failure.
import type { Callback } from "callback.flow"
export const readAsync = (
address: string,
callback: Callback<Error, string>
): void => {
if (isValid(address)) {
// ....
callback(null, content)
} else {
callback(new Error("Invalid Address"))
}
}
npm install callback.flow