diff --git a/langchain-core/src/utils/signal.ts b/langchain-core/src/utils/signal.ts index 535faaa8502f..b0a7871470e6 100644 --- a/langchain-core/src/utils/signal.ts +++ b/langchain-core/src/utils/signal.ts @@ -8,11 +8,11 @@ export async function raceWithSignal( return Promise.race([ promise, new Promise((_, reject) => { - // Must be inside of the promise to avoid a race condition + signal.addEventListener("abort", () => reject(new Error("Aborted"))); + // Must be here inside the promise to avoid a race condition if (signal.aborted) { return reject(new Error("Aborted")); } - signal.addEventListener("abort", () => reject(new Error("Aborted"))); }), ]); }