Skip to content

Commit b18c731

Browse files
committed
Fix UnhandledPromiseRejectionWarning in result
It is possible to subscribe to `Result` with an observer which consists of three functions: `onNext`, `onError` and `onCompleted`. All three are optional. However result was not handling absence of `onCompleted` correctly which resulted in a failed promise and `UnhandledPromiseRejectionWarning`.
1 parent fe6190b commit b18c731

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/v1/internal/connection-holder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class ConnectionHolder {
6161

6262
/**
6363
* Notify this holder that single party does not require current connection any more.
64-
* @return {Promise<Connection>} promise resolved with the current connection.
64+
* @return {Promise<Connection>} promise resolved with the current connection, never a rejected promise.
6565
*/
6666
releaseConnection() {
6767
if (this._referenceCount === 0) {

src/v1/result.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
import ResultSummary from './result-summary';
2121
import {EMPTY_CONNECTION_HOLDER} from './internal/connection-holder';
2222

23+
const DEFAULT_ON_ERROR = error => {
24+
console.log('Uncaught error when processing result: ' + error);
25+
};
26+
const DEFAULT_ON_COMPLETED = summary => {
27+
};
28+
2329
/**
2430
* A stream of {@link Record} representing the result of a statement.
2531
* @access public
@@ -102,12 +108,12 @@ class Result {
102108
* @return
103109
*/
104110
subscribe(observer) {
105-
const onCompletedOriginal = observer.onCompleted;
106111
const self = this;
107-
const onCompletedWrapper = (metadata) => {
108112

113+
const onCompletedOriginal = observer.onCompleted || DEFAULT_ON_COMPLETED;
114+
const onCompletedWrapper = (metadata) => {
109115
const additionalMeta = self._metaSupplier();
110-
for(let key in additionalMeta) {
116+
for (let key in additionalMeta) {
111117
if (additionalMeta.hasOwnProperty(key)) {
112118
metadata[key] = additionalMeta[key];
113119
}
@@ -122,10 +128,7 @@ class Result {
122128
};
123129
observer.onCompleted = onCompletedWrapper;
124130

125-
const onErrorOriginal = observer.onError || (error => {
126-
console.log("Uncaught error when processing result: " + error);
127-
});
128-
131+
const onErrorOriginal = observer.onError || DEFAULT_ON_ERROR;
129132
const onErrorWrapper = error => {
130133
// notify connection holder that the used connection is not needed any more because error happened
131134
// and result can't bee consumed any further; call the original onError callback after that

test/v1/result.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,16 @@ describe('result stream', () => {
6060
done()
6161
});
6262
});
63+
64+
it('should handle missing onCompleted', done => {
65+
session.run('RETURN 1').subscribe({
66+
onNext: record => {
67+
expect(record.get(0).toInt()).toEqual(1);
68+
done();
69+
},
70+
onError: error => {
71+
console.log(error);
72+
}
73+
});
74+
});
6375
});

0 commit comments

Comments
 (0)