use-async-resource
Version:
A custom React hook for simple data fetching with React Suspense
15 lines (13 loc) • 443 B
text/typescript
// mimics how Suspense treats thrown promises
export async function suspendFor<T extends () => unknown>(throwablePromise: T) {
// initially, the data reader throws the new promise
expect(throwablePromise).toThrow();
try {
// calling the function will throw the promise
throwablePromise();
} catch(tp) {
// Suspense will catch it and wait its fulfilment
await tp;
expect('awaited successfully').toBeTruthy();
}
}