From 54390e55374a4098e8377f70d7d25c2c35cd3e2b Mon Sep 17 00:00:00 2001 From: WTCT-TOP Date: Mon, 2 Dec 2024 10:01:08 -0500 Subject: [PATCH 1/5] Add example for `request` + "Garbage Collection" --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 890d82c2f7c..6729b2c04f3 100644 --- a/README.md +++ b/README.md @@ -294,6 +294,22 @@ const headers = await fetch(url) .then(res => res.headers) ``` +The same applies for `request` too: +```js +// Do +const response = await request(url) + .then(res => { + response.body.dump().catch(err => { + console.error('Error while dumping the body'); + }); + return res.headers; + }); + +// Do not +const response = await request(url) + .then(res => res.headers); +``` + However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details. ```js From d05c50f7d02ae719a3e8db3ae077dd5ce23e52a6 Mon Sep 17 00:00:00 2001 From: WTCT-TOP Date: Mon, 2 Dec 2024 10:17:52 -0500 Subject: [PATCH 2/5] `dump` never throws without a signal --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 6729b2c04f3..60fbeccd1c3 100644 --- a/README.md +++ b/README.md @@ -299,9 +299,7 @@ The same applies for `request` too: // Do const response = await request(url) .then(res => { - response.body.dump().catch(err => { - console.error('Error while dumping the body'); - }); + response.body.dump(); return res.headers; }); From e6a967c9fbff76d461885c8f3f50939e4022795e Mon Sep 17 00:00:00 2001 From: WTCT-TOP Date: Mon, 2 Dec 2024 11:13:39 -0500 Subject: [PATCH 3/5] fix code (response !== res) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 60fbeccd1c3..a0888a370f1 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,7 @@ The same applies for `request` too: // Do const response = await request(url) .then(res => { - response.body.dump(); + res.body.dump(); return res.headers; }); From f49ec50a03e0a4880a3a57c755c8cd779356031e Mon Sep 17 00:00:00 2001 From: WTCT-TOP Date: Sat, 7 Dec 2024 00:03:45 -0500 Subject: [PATCH 4/5] async await --- README.md | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index a0888a370f1..5e31af133ed 100644 --- a/README.md +++ b/README.md @@ -281,31 +281,23 @@ stalls or deadlocks when running out of connections. ```js // Do -const headers = await fetch(url) - .then(async res => { - for await (const chunk of res.body) { - // force consumption of body - } - return res.headers - }) +const { body, headers } = await fetch(url); +for await (const chunk of body) { + // force consumption of body +} // Do not -const headers = await fetch(url) - .then(res => res.headers) +const { headers } = await fetch(url); ``` The same applies for `request` too: ```js // Do -const response = await request(url) - .then(res => { - res.body.dump(); - return res.headers; - }); +const { body, headers } = await request(url) +await res.body.dump(); // Do not -const response = await request(url) - .then(res => res.headers); +const { headers } = await request(url); ``` However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details. From 3649b767f29279729b4b7e81ec2ebd2a95e320c5 Mon Sep 17 00:00:00 2001 From: WTCT-TOP Date: Sat, 7 Dec 2024 00:05:58 -0500 Subject: [PATCH 5/5] add "force consumption of body" comment --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5e31af133ed..23891fe365d 100644 --- a/README.md +++ b/README.md @@ -293,8 +293,8 @@ const { headers } = await fetch(url); The same applies for `request` too: ```js // Do -const { body, headers } = await request(url) -await res.body.dump(); +const { body, headers } = await request(url); +await res.body.dump(); // force consumption of body // Do not const { headers } = await request(url);