Skip to content

Commit 39da372

Browse files
committed
refactor(node-sdk): simplify event flushing timeout handling
Use the new `withTimeout()` utility to replace manual Promise.race() implementation in flusher, improving error handling and readability
1 parent 81ec58c commit 39da372

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

packages/node-sdk/src/flusher.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { constants } from "os";
22

33
import { END_FLUSH_TIMEOUT_MS } from "./config";
4+
import { withTimeout, TimeoutError } from "./utils";
45

56
type Callback = () => Promise<void>;
67

@@ -20,21 +21,18 @@ export function subscribe(
2021
state = false;
2122

2223
try {
23-
const result = await Promise.race([
24-
new Promise((resolve) => setTimeout(() => resolve(true), timeout)),
25-
callback(),
26-
]);
27-
28-
if (result === true) {
24+
await withTimeout(callback(), timeout);
25+
} catch (error) {
26+
if (error instanceof TimeoutError) {
2927
console.error(
3028
"[Bucket SDK] Timeout while flushing events on process exit.",
3129
);
30+
} else {
31+
console.error(
32+
"[Bucket SDK] An error occurred while flushing events on process exit.",
33+
error,
34+
);
3235
}
33-
} catch (error) {
34-
console.error(
35-
"[Bucket SDK] An error occurred while flushing events on process exit.",
36-
error,
37-
);
3836
}
3937

4038
state = true;

0 commit comments

Comments
 (0)