Skip to content

Commit 27303a1

Browse files
committed
Lazily create promise to avoid rejection warnings
Connection exposes a promise resolved/rejected after INIT message. This is done via `#initializationCompleted()` function. This commit makes promise creation lazy because otherwise it results in `UnhandledPromiseRejectionWarning` if noone calls `#initializationCompleted()`.
1 parent e93cdf0 commit 27303a1

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/v1/internal/connector.js

+25-6
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,12 @@ class ConnectionState {
471471
*/
472472
constructor(connection) {
473473
this._connection = connection;
474+
475+
this._initialized = false;
476+
this._initializationError = null;
477+
474478
this._resolvePromise = null;
475-
this._promise = new Promise(resolve => {
476-
this._resolvePromise = resolve;
477-
});
479+
this._rejectPromise = null;
478480
}
479481

480482
/**
@@ -490,7 +492,11 @@ class ConnectionState {
490492
}
491493
},
492494
onError: error => {
493-
this._resolvePromise(Promise.reject(error));
495+
this._initializationError = error;
496+
if (this._rejectPromise) {
497+
this._rejectPromise(error);
498+
this._rejectPromise = null;
499+
}
494500
if (observer && observer.onError) {
495501
observer.onError(error);
496502
}
@@ -499,7 +505,11 @@ class ConnectionState {
499505
if (metaData && metaData.server) {
500506
this._connection.setServerVersion(metaData.server);
501507
}
502-
this._resolvePromise(this._connection);
508+
this._initialized = true;
509+
if (this._resolvePromise) {
510+
this._resolvePromise(this._connection);
511+
this._resolvePromise = null;
512+
}
503513
if (observer && observer.onCompleted) {
504514
observer.onCompleted(metaData);
505515
}
@@ -512,7 +522,16 @@ class ConnectionState {
512522
* @return {Promise<Connection>} the result of connection initialization.
513523
*/
514524
initializationCompleted() {
515-
return this._promise;
525+
if (this._initialized) {
526+
return Promise.resolve(this._connection);
527+
} else if (this._initializationError) {
528+
return Promise.reject(this._initializationError);
529+
} else {
530+
return new Promise((resolve, reject) => {
531+
this._resolvePromise = resolve;
532+
this._rejectPromise = reject;
533+
});
534+
}
516535
}
517536
}
518537

test/internal/connector.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {Neo4jError} from '../../src/v1/error';
2626
import sharedNeo4j from '../internal/shared-neo4j';
2727
import {ServerVersion} from '../../src/v1/internal/server-version';
2828

29-
fdescribe('connector', () => {
29+
describe('connector', () => {
3030

3131
it('should read/write basic messages', done => {
3232
// Given

0 commit comments

Comments
 (0)