Refine README example (#463)

This reverts commit fa6548ed31 (#441).

The autoClose option has been true by default since at least Node.js
v6.0.0. There is no need to set it once more.

Instead, make the example more realistic by handling stream outcomes
using a promise.

See #375.
This commit is contained in:
Timothy Gu 2018-05-27 21:26:22 -07:00 committed by David Frank
parent 5bc23d81cf
commit 2b7e1ab27f
1 changed files with 12 additions and 3 deletions

View File

@ -80,10 +80,19 @@ fetch('http://domain.invalid/')
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
.then(res => {
const dest = fs.createWriteStream('./octocat.png', {
autoClose: true,
return new Promise((resolve, reject) => {
const dest = fs.createWriteStream('./octocat.png');
res.body.pipe(dest);
res.body.on('error', err => {
reject(err);
});
dest.on('finish', () => {
resolve();
});
dest.on('error', err => {
reject(err);
});
});
res.body.pipe(dest);
});
// buffer