Skip to content

Commit b544bd5

Browse files
[Backport 8.14] Update asStream code example (#2244)
#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 <joshua.mock@elastic.co>
1 parent 07e8d4e commit b544bd5

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

docs/examples/asStream.asciidoc

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[[as_stream_examples]]
22
=== asStream
33

4-
Instead of getting the parsed body back, you will get the raw Node.js stream of
4+
Instead of getting the parsed body back, you will get the raw Node.js stream of
55
data.
66

77
[source,js]
@@ -57,28 +57,18 @@ async function run () {
5757
asStream: true
5858
})
5959
60-
// stream async iteration, available in Node.js ≥ 10
6160
let payload = ''
62-
body.setEncoding('utf8')
61+
result.setEncoding('utf8')
6362
for await (const chunk of result) {
6463
payload += chunk
6564
}
6665
console.log(JSON.parse(payload))
67-
68-
// classic stream callback style
69-
let payload = ''
70-
result.setEncoding('utf8')
71-
result.on('data', chunk => { payload += chunk })
72-
result.on('error', console.log)
73-
result.on('end', () => {
74-
console.log(JSON.parse(payload))
75-
})
7666
}
7767
7868
run().catch(console.log)
7969
----
8070

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

8474
[source,js]

0 commit comments

Comments
 (0)