callable-deferred-promise
v1.0.0
Published
A thin wrapper class to create a callable deferred `Promise`.
Downloads
6
Readme
Callable Deferred Promise
A thin wrapper class to create a callable deferred Promise
.
Features
- Zero-dependency
- TypeScript support
- ESM/CJS/UMD support
Example
import CallableDeferredPromise from "callable-deferred-promise"
type Options = { foo: "bar" }
const performSomeAsyncTask = async (
options?: Options | undefined,
): Promise<void> => {
// ...
}
const it = {
resolves: new CallableDeferredPromise<[Options], void>(
(args: [Options] | undefined) => resolve => {
// `args` is `undefined` if it wasn't called
if (args) {
const [options] = args
resolve(performSomeAsyncTask(options))
} else {
resolve(performSomeAsyncTask())
}
},
),
}
// use without a call
await it.resolves
// use with a call
const options: Options = { foo: "bar" }
await it.resolves(options)