Skip to content

Commit 7f55e2d

Browse files
committed
Make sure SSEResponse end() resolves after calling res.end()
The res.end callback was not always being called. It should be enough to end the response and resolve now, no need to for the caller to wait for res.end() to fire its callback. This was causing dependent Promises in KafkaSSE to wait indefinetly when disconnecting.
1 parent d2f4848 commit 7f55e2d

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

lib/SSEResponse.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,32 @@ class SSEResponse {
135135

136136

137137
/**
138-
* Ends the response.
138+
* Ends the HTTP response.
139139
*/
140140
end() {
141141
if (!this.res) {
142142
return P.resolve();
143143
}
144+
144145
if (this._resFinished()) {
145146
delete this.res;
146147
return P.resolve();
147148
}
149+
150+
const res = this.res;
151+
delete this.res;
152+
148153
return new P((resolve, reject) => {
149-
const res = this.res;
150-
if (this._resFinished()) {
151-
delete this.res;
152-
return resolve();
153-
}
154-
delete this.res;
155-
res.on('error', reject);
154+
res.once('error', reject);
156155
try {
157-
if (res.end(resolve) === false) {
158-
resolve();
159-
}
156+
res.end();
157+
resolve();
160158
} catch(e) {
161159
reject(e);
162160
}
163161
});
164162
}
165163

166-
167164
_resFinished() {
168165
const res = this.res;
169166
if (!res || res.finished || (res.connection && res.connection.destroyed)) {

0 commit comments

Comments
 (0)