Skip to content

Commit aed51aa

Browse files
fix(js-sdk): emit error event on watcher timeout to prevent start() hanging
Timeout paths in both WebSocket and pollLoop were calling close() without emitting an error event, which would cause the start() Promise to hang indefinitely. Now emits an error event before closing so the Promise properly resolves. Co-Authored-By: gaurav <gauravchadha1676@gmail.com>
1 parent abeb2a7 commit aed51aa

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

apps/js-sdk/firecrawl/src/v2/watcher.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ export class Watcher extends EventEmitter {
179179
} catch {
180180
// ignore
181181
}
182-
if (timeoutMs && Date.now() - startTs > timeoutMs) this.close();
182+
if (timeoutMs && Date.now() - startTs > timeoutMs) {
183+
this.emit("error", { status: "failed", data: [], error: "Watcher timeout", id: this.jobId });
184+
this.close();
185+
}
183186
};
184187
ws.onerror = () => {
185188
this.emit("error", { status: "failed", data: [], error: "WebSocket error", id: this.jobId });
@@ -263,7 +266,11 @@ export class Watcher extends EventEmitter {
263266
} catch {
264267
// ignore polling errors
265268
}
266-
if (timeoutMs && Date.now() - startTs > timeoutMs) break;
269+
if (timeoutMs && Date.now() - startTs > timeoutMs) {
270+
this.emit("error", { status: "failed", data: [], error: "Watcher timeout", id: this.jobId });
271+
this.close();
272+
break;
273+
}
267274
await new Promise((r) => setTimeout(r, Math.max(1000, this.pollInterval * 1000)));
268275
}
269276
}

0 commit comments

Comments
 (0)