title: Writing the stream returned by node-fetch
body: <p>the README contains the following code as an example of writing a file fetched:</p>
  
  <pre><code>fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
      .then(res =&gt; {
          const dest = fs.createWriteStream('./octocat.png');
          res.body.pipe(dest);
      });
  </code></pre>
  
  <p>to which I would concatenate another <code>.then()</code> to use the file written to disk.  in reality it appears that my code runs before the file has finished writing to disk, so it occurs to me that the promise returned by the above code should take into consideration the issuance of the 'end' event on the stream.  maybe something like this:</p>
  
  <pre><code>fetch(url).then(res =&gt; new Promise((resolve, reject) =&gt; {
      const dest = fs.createWriteStream(fn);
      res.body.pipe(dest);
      res.body.on('end', () =&gt; resolve());
      dest.on('error', reject);
  }));
  </code></pre>
  
  <p>but when I try to run this code it complains:</p>
  
  <blockquote>
    <p>TypeError: res.body.on is not a function</p>
  </blockquote>
  
  <p>which makes sense given that <code>res.body</code> looks like this:</p>
  
  <pre><code>{ pipe: [Function: pipe] }
  </code></pre>
  
  <p>which is to say, is not a stream at all</p>
  
  <p>two questions:
  1) how can I get access to the actual stream?
  2) if I have no access to it, how can I know when the read stream has closed so I can resolve?</p>
  
  <p><strong>p.s.</strong></p>
  
  <p>and no, I can't get the stream from <code>.pipe()</code>, which returns undefined</p>