Skip to content

Commit

Permalink
Ignore thrown errors after aborting a signal
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Aug 1, 2024
1 parent 1c6c2b6 commit de8df00
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions langchain-core/src/utils/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ export async function raceWithSignal<T>(
return promise;
}
return Promise.race([
promise,
promise.catch<T>((err) => {
if (!signal?.aborted) {
throw err;
} else {
return undefined as T;
}
}),
new Promise<never>((_, reject) => {
signal.addEventListener("abort", () => reject(new Error("Aborted")));
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"));
reject(new Error("Aborted"));
}
}),
]);
Expand Down

0 comments on commit de8df00

Please sign in to comment.