node-pg-query-stream
node-pg-query-stream copied to clipboard
await:ing in for-await loops short-circuits the stream
Hi,
I'm trying my luck using async iterators, with the end goal to use for-await
.
However, a peculiar behaviour occurred while awaiting within the loop.
I am not sure if this is an internal bug or if a change (which occurred in v11.5) changed the behaviour of async iterator which in turn either pg-cursor or pg-query-stream did not follow.
I posted a bug report in node repo because I wanted to understand the change (intended or not), but I'll double post the bug here for clarity and perhaps its an easy fix outside of internals?
Here's a reproducible:
const assert = require('assert')
const { Client } = require('pg')
const QueryStream = require('pg-query-stream')
async function main () {
const client = new Client()
await client.connect()
const sql = 'select * from generate_series(1, $1)'
const n = 10 // num rows to generate
const query = new QueryStream(sql, [n])
const stream = client.query(query)
let i = 0
for await (const row of stream) {
console.log(stream._readableState.length)
i += 1
await new Promise((resolve) => setTimeout(() => resolve(), 0))
// ^-- This await breaks it all in v10.16, v11.5+ but works in from v11.0 to v.11.4
}
await client.end()
assert.strictEqual(i, n)
}
if (!module.parent) {
main()
}
$ node -v
v11.4.0
$ node what.js
9
8
7
6
5
4
3
2
1
0
$ node -v
v11.5.0
$ node what.js
9
8
(node:21383) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
2 !== 10
at main (/Users/fl0w/async-iterator-stream/what.js:24:10)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
Refs: https://github.com/nodejs/node/issues/29410
There is a correct implementation of this module that fixes this issue here: https://github.com/brianc/node-pg-query-stream/issues/52#issuecomment-561644112