'Success'}
+ error={(err) => Error: {(err as Error).message}
}
+ />,
+ )
+
+ await screen.findByText('Error: Sync Error')
+ })
})
diff --git a/lib/components/AsyncBlock/index.tsx b/lib/components/AsyncBlock/index.tsx
index 390e22b..ac977a3 100644
--- a/lib/components/AsyncBlock/index.tsx
+++ b/lib/components/AsyncBlock/index.tsx
@@ -1,10 +1,4 @@
-import {
- useEffect,
- useState,
- ReactNode,
- useRef,
- useCallback,
-} from 'react'
+import { useEffect, useState, ReactNode, useRef, useCallback } from 'react'
/**
* AsyncBlock component – Declaratively renders asynchronous content with pending, success, and error states.
@@ -94,7 +88,20 @@ export const AsyncBlock = ({
}
// Pass the signal to the promise function, but make it optional
- promiseFn(signal)
+ let promise: Promise
+ try {
+ promise = promiseFn(signal)
+ } catch (e) {
+ if (isMounted.current) {
+ if (timer) clearTimeout(timer)
+ setErr(e)
+ setState('error')
+ onError?.(e)
+ }
+ return
+ }
+
+ promise
.then((result) => {
if (signal.aborted) return