Skip to content

Commit

Permalink
[Backport 8.14] Update asStream code example (#2244)
Browse files Browse the repository at this point in the history
#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 45e3c06)

Co-authored-by: Josh Mock <[email protected]>
  • Loading branch information
github-actions[bot] and JoshMock authored Apr 30, 2024
1 parent 07e8d4e commit b544bd5
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions docs/examples/asStream.asciidoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[[as_stream_examples]]
=== asStream

Instead of getting the parsed body back, you will get the raw Node.js stream of
Instead of getting the parsed body back, you will get the raw Node.js stream of
data.

[source,js]
Expand Down Expand Up @@ -57,28 +57,18 @@ 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)
----

TIP: This can be useful if you need to pipe the {es}'s response to a proxy, or
TIP: This can be useful if you need to pipe the {es}'s response to a proxy, or
send it directly to another source.

[source,js]
Expand Down

0 comments on commit b544bd5

Please sign in to comment.