title: Promises not going in order of the "then()"s? (nodejs)
body: <p>Why does the then() where it says console.log('THIS COMES BEFORE THE WRITING AND EXTRACTING'); statement come before the writing and extracting to PDFs? I'm new to Promises, so I am a bit confused by this. I want that then() statement to happen AFTER the writing and parsing happens.</p>
  
  <pre><code>function writeToPDF(data, fileName) {
    return new Promise((resolve, reject) =&gt; {
      data.pipe(fs.createWriteStream(fileName), err =&gt; {
        reject();
      });
      data.on('end', () =&gt; {
        resolve();
      })
    });
  }
  ​
  function extractPDF(pdf) {
    return new Promise((resolve, reject) =&gt; {
      extract(pdf, {splitPages: false}, (err, text) =&gt; {
        if (err) {
          console.log(err);
        } else {
          resolve(text);
        }
      });
    });
  }
  ​
  request(link).then((success, failure) =&gt; {
    let fileName = './name-' + Date.now() + '.pdf';
    writeToPDF(data, fileName).then(() =&gt; {
      extractPDF(fileName).then((text) =&gt; {
        arrayOfDocuments.push(text);
      })
    }, () =&gt; {
      //handle error
    });
  }).then(() =&gt; {
    console.log('THIS COMES BEFORE THE WRITING AND EXTRACTING');
  });
  </code></pre>