[Backport 8.14] Update asStream code example (#2244)

https://github.com/elastic/elasticsearch-js/issues/2241 notes that there
is no body attribute on a response. This is mostly just a typo in the
example, as `result` itself is a readable stream, unless `meta: true` is
passed, in which case `result.body` will be a readable stream.

Also dropped the callback-style stream processing example as it's a bit
outdated.

(cherry picked from commit 45e3c0657a)

Co-authored-by: Josh Mock <joshua.mock@elastic.co>
This commit is contained in:
github-actions[bot]
2024-04-30 13:59:54 -05:00
committed by GitHub
parent 07e8d4eec7
commit b544bd56ad

View File

@ -57,22 +57,12 @@ async function run () {
asStream: true
})
// stream async iteration, available in Node.js ≥ 10
let payload = ''
body.setEncoding('utf8')
result.setEncoding('utf8')
for await (const chunk of result) {
payload += chunk
}
console.log(JSON.parse(payload))
// classic stream callback style
let payload = ''
result.setEncoding('utf8')
result.on('data', chunk => { payload += chunk })
result.on('error', console.log)
result.on('end', () => {
console.log(JSON.parse(payload))
})
}
run().catch(console.log)