Skip to content
This repository has been archived by the owner on Dec 27, 2019. It is now read-only.

await:ing in for-await loops short-circuits the stream #56

Open
fl0w opened this issue Sep 3, 2019 · 1 comment
Open

await:ing in for-await loops short-circuits the stream #56

fl0w opened this issue Sep 3, 2019 · 1 comment

Comments

@fl0w
Copy link

fl0w commented Sep 3, 2019

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: nodejs/node#29410

@matthieusieben
Copy link

There is a correct implementation of this module that fixes this issue here: #52 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants