Skip to content

Commit 950dd49

Browse files
stainless-botRobertCraigie
authored andcommitted
fix(streaming): do not abort successfully completed streams (#53)
1 parent 97bd635 commit 950dd49

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/streaming.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Ite
5151
}
5252

5353
async *[Symbol.asyncIterator](): AsyncIterator<Item, any, undefined> {
54+
let done = false;
5455
try {
5556
for await (const sse of this.iterMessages()) {
5657
if (sse.event === 'completion') {
@@ -75,13 +76,14 @@ export class Stream<Item> implements AsyncIterable<Item>, APIResponse<Stream<Ite
7576
throw APIError.generate(undefined, errJSON, errMessage, this.responseHeaders);
7677
}
7778
}
79+
done = true;
7880
} catch (e) {
7981
// If the user calls `stream.controller.abort()`, we should exit without throwing.
8082
if (e instanceof Error && e.name === 'AbortError') return;
8183
throw e;
8284
} finally {
8385
// If the user `break`s, abort the ongoing request.
84-
this.controller.abort();
86+
if (!done) this.controller.abort();
8587
}
8688
}
8789
}

0 commit comments

Comments
 (0)