简体   繁体   English

承诺不按“ then()”的顺序进行吗? (nodejs)

[英]Promises not going in order of the “then()”s? (nodejs)

Why does the then() where it says console.log('THIS COMES BEFORE THE WRITING AND EXTRACTING'); 为什么then()表示console.log('这是在写和提取之前发生的'); statement come before the writing and extracting to PDFs? 声明在撰写和解压缩为PDF之前出现? I'm new to Promises, so I am a bit confused by this. 我是Promises的新手,所以对此感到有些困惑。 I want that then() statement to happen AFTER the writing and parsing happens. 我希望then()语句在编写和解析之后发生。

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');
});

You are nesting your then values and actually have two different then routes — for lack of a better term. 您正在嵌套您的then值,实际上有两条不同的then路线-缺乏更好的条件。

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 . 至于javascript的来讲,一旦第一then在你的解决request的方法,它可以移动到下一个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. 在这里,解决了writeToPdf之后,由于已解决了该级别的上一个承诺,因此承诺链的该部分随后移至console.log语句。 That make sense? 有道理?

The request(link).then(...) block comprises an outer promise chain and two levels of nested promise chain. request(link).then(...)块包括一个外部promise链和两层嵌套的promise链。

If you write a fat-arrow function with a {block} , returns are not explicit , therefore : 如果您使用{block}编写一个胖箭头函数,则返回不是显式的 ,因此:

  • neither of the promises derived from writeToPDF() or extractPDF() is returned, writeToPDF()extractPDF()派生的任何承诺writeToPDF()返回,
  • 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. console.log('THIS COMES BEFORE ...')仅取决于request(link)返回的诺言的兑现,并且保证在PDF内容开始之后但完成之前发生。
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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM