mirror of
https://github.com/whscullin/apple2js.git
synced 2024-01-12 14:14:38 +00:00
d7cb6997d1
This change adds `spawn` which takes a no-argument, promise-returning function, calls it, and returns `void`. This makes it easy to call async blocks from `useEffect` and other places that don't take async functions, but also makes such calls explicit.
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
/**
|
|
* Converts a function type returning a `Promise` to a function type returning `void`.
|
|
*/
|
|
export type NoAwait<F extends (...args: unknown[]) => Promise<unknown>> =
|
|
(...args: Parameters<F>) => void;
|
|
|
|
/**
|
|
* Signals that the argument returns a `Promise` that is intentionally not being awaited.
|
|
*/
|
|
export function noAwait<F extends (...args: unknown[]) => Promise<unknown>>(f: F): NoAwait<F> {
|
|
return f as NoAwait<F>;
|
|
}
|
|
|
|
/**
|
|
* Calls the given `Promise`-returning function and returns void, signalling that it is
|
|
* explicitly not awaited.
|
|
*/
|
|
export function spawn(f: () => Promise<unknown>): void {
|
|
noAwait(f)();
|
|
}
|
|
|
|
/**
|
|
* Utility class that allows a promise to be passed to a
|
|
* service to be resolved.
|
|
*/
|
|
|
|
export class Ready {
|
|
onError: (value?: unknown) => void;
|
|
onReady: (value?: unknown) => void;
|
|
|
|
ready: Promise<unknown>;
|
|
|
|
constructor(private errorHandler = console.error) {
|
|
this.ready = new Promise((resolve, reject) => {
|
|
this.onReady = resolve;
|
|
this.onError = reject;
|
|
}).catch(this.errorHandler);
|
|
}
|
|
}
|