Skip to content
Merged
Changes from 2 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
17 changes: 6 additions & 11 deletions apps/site/pages/en/learn/getting-started/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ try {

```js
import { Writable } from '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 +148,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