From b544bd56adef95c7470dd498bbfa4f052e40edb4 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:59:54 -0500 Subject: [PATCH] [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 45e3c0657ab8ff1db8c7f74be9a19a6a0549b971) Co-authored-by: Josh Mock --- docs/examples/asStream.asciidoc | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/docs/examples/asStream.asciidoc b/docs/examples/asStream.asciidoc index e77025fcf..e27c0a1b1 100644 --- a/docs/examples/asStream.asciidoc +++ b/docs/examples/asStream.asciidoc @@ -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] @@ -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]