Skip to content

Commit

Permalink
fix(streaming): do not abort successfully completed streams (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and RobertCraigie committed Jul 13, 2023
1 parent 97bd635 commit 950dd49
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/streaming.ts
Expand Up @@ -51,6 +51,7 @@ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Ite
}

async *[Symbol.asyncIterator](): AsyncIterator<Item, any, undefined> {
let done = false;
try {
for await (const sse of this.iterMessages()) {
if (sse.event === 'completion') {
Expand All @@ -75,13 +76,14 @@ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Ite
throw APIError.generate(undefined, errJSON, errMessage, this.responseHeaders);
}
}
done = true;
} catch (e) {
// If the user calls `stream.controller.abort()`, we should exit without throwing.
if (e instanceof Error && e.name === 'AbortError') return;
throw e;
} finally {
// If the user `break`s, abort the ongoing request.
this.controller.abort();
if (!done) this.controller.abort();
}
}
}
Expand Down

0 comments on commit 950dd49

Please sign in to comment.