Skip to content

Commit 98cd59e

Browse files
authored
Return promise on cursor end (#2589)
* Return promise on cursor end * Remove redudant if
1 parent 92b4d37 commit 98cd59e

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Diff for: packages/pg-cursor/index.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,15 @@ class Cursor extends EventEmitter {
208208
}
209209

210210
if (!this.connection || this.state === 'done') {
211-
if (cb) {
212-
return setImmediate(cb)
213-
} else {
214-
return
215-
}
211+
setImmediate(cb)
212+
return promise
216213
}
217214

218215
this._closePortal()
219216
this.state = 'done'
220-
if (cb) {
221-
this.connection.once('readyForQuery', function () {
222-
cb()
223-
})
224-
}
217+
this.connection.once('readyForQuery', function () {
218+
cb()
219+
})
225220

226221
// Return the promise (or undefined)
227222
return promise

Diff for: packages/pg-cursor/test/close.js

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ describe('close', function () {
2323
})
2424
})
2525

26+
it('can close a finished cursor a promise', function (done) {
27+
const cursor = new Cursor(text)
28+
this.client.query(cursor)
29+
cursor.read(100, (err) => {
30+
assert.ifError(err)
31+
cursor.close().then(() => {
32+
this.client.query('SELECT NOW()', done)
33+
})
34+
})
35+
})
36+
2637
it('closes cursor early', function (done) {
2738
const cursor = new Cursor(text)
2839
this.client.query(cursor)

0 commit comments

Comments
 (0)