0

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.

function writeToPDF(data, fileName) {
  return new Promise((resolve, reject) => {
    data.pipe(fs.createWriteStream(fileName), err => {
      reject();
    });
    data.on('end', () => {
      resolve();
    })
  });
}
​
function extractPDF(pdf) {
  return new Promise((resolve, reject) => {
    extract(pdf, {splitPages: false}, (err, text) => {
      if (err) {
        console.log(err);
      } else {
        resolve(text);
      }
    });
  });
}
​
request(link).then((success, failure) => {
  let fileName = './name-' + Date.now() + '.pdf';
  writeToPDF(data, fileName).then(() => {
    extractPDF(fileName).then((text) => {
      arrayOfDocuments.push(text);
    })
  }, () => {
    //handle error
  });
}).then(() => {
  console.log('THIS COMES BEFORE THE WRITING AND EXTRACTING');
});
ThePumpkinMaster
  • 1,561
  • 5
  • 16
  • 25

2 Answers2

0

You are nesting your then values and actually have two different then routes — for lack of a better term.

So far as the javascript is concerned, once the first then is resolved in your request method, it can move on to the next then. Here, once writeToPdf is resolved, that part of the promise chain then moves on to the console.log statement since the previous promise at that level has been resolved. That make sense?

Pytth
  • 3,470
  • 17
  • 23
  • How do I get it to do what I want then? I want to writeToPDF and extractPDF and THEN go to the console.log() statement. – ThePumpkinMaster Jun 16 '16 at 17:46
  • writeToPdf(data, fileName).then(extractPdf).then( // fn for console.log ) – Pytth Jun 16 '16 at 17:50
  • What if I have an array of requests? For example, I have an array of two requests, and one of them is a normal txt file so I wouldn't parse it. I want to run the console.log() statement AFTER both of them are completed. How do I do this? – ThePumpkinMaster Jun 16 '16 at 18:02
  • Have you looked at Promise.all? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – Pytth Jun 16 '16 at 18:04
0

The request(link).then(...) block comprises an outer promise chain and two levels of nested promise chain.

If you write a fat-arrow function with a {block}, returns are not explicit, therefore :

  • neither of the promises derived from writeToPDF() or extractPDF() is returned,
  • the outer promise chain is not informed of the existence of inner promises or their settlement,
  • console.log('THIS COMES BEFORE ...') relies solely on the fulfillment of the promise returned by request(link), and is guaranteed to occur after PDF stuff commences but before it completes.
request(link).then((success, failure) => {
  let fileName = './name-' + Date.now() + '.pdf';
  return writeToPDF(data, fileName).then(() => {
//^^^^^^
    return extractPDF(fileName).then((text) => {
//  ^^^^^^
      arrayOfDocuments.push(text);
    })
  }, () => {
    //handle error
  });
}).then(() => {
  console.log('THIS COMES BEFORE THE WRITING AND EXTRACTING');
});
Community
  • 1
  • 1
Roamer-1888
  • 18,384
  • 5
  • 29
  • 42