We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8b511ad commit 8e39686Copy full SHA for 8e39686
src/callWithHooks.ts
@@ -5,17 +5,26 @@
5
* @param onDone
6
* @param onError
7
*/
8
-export default async function callWithHooks <T> (
+const callWithHooks = <T>(
9
call: () => T | Promise<T>,
10
onDone: () => void,
11
onError: () => void = onDone,
12
-): Promise<T> {
13
- try {
14
- const value = await call();
15
- onDone();
16
- return Promise.resolve(value);
17
- } catch (error) {
+): Promise<T> => {
+ const handleError = (error: unknown) => {
18
onError();
19
return Promise.reject(error);
+ };
+
+ try {
+ return Promise.resolve(call())
20
+ .then((value: T) => {
21
+ onDone();
22
+ return Promise.resolve(value);
23
+ })
24
+ .catch(handleError);
25
+ } catch (error) {
26
+ return handleError(error);
27
}
-}
28
+};
29
30
+export default callWithHooks;
0 commit comments