Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions apps/site/pages/en/learn/getting-started/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ try {
[Streams](https://nodejs.org/docs/v22.14.0/api/stream.html#stream) is a feature in Node.js that allows you to read and write chunks of data.

```js
import { Writable } from 'stream';
import { Writable } from 'node:stream';

import { stream } from 'undici';

async function fetchGitHubRepos() {
const url = 'https://api.github.com/users/nodejs/repos';

const { statusCode } = await stream(
await stream(
url,
{
method: 'GET',
Expand All @@ -149,35 +149,31 @@ async function fetchGitHubRepos() {
Accept: 'application/json',
},
},
() => {
res => {
let buffer = '';

return new Writable({
write(chunk, encoding, callback) {
buffer += chunk.toString();

callback();
},
final(callback) {
try {
const json = JSON.parse(buffer);
console.log(
'Repository Names:',
json.map(repo => repo.name)
);
buffer = '';
} catch (error) {
console.error('Error parsing JSON:', error);
}

callback();
},
final(callback) {
console.log('Stream processing completed.');
console.log(`Response status: ${res.statusCode}`);
callback();
},
});
}
);

console.log(`Response status: ${statusCode}`);
}

fetchGitHubRepos().catch(console.error);
Expand Down