简体   繁体   English

nodejs-使用嵌套的promise帮助“分散”读取的文件

[英]nodejs - Help “promisifying” a file read with nested promises

So I've recently delved into trying to understand promises and the purpose behind them due to javascripts asynchronous behavior. 因此,由于JavaScript的异步行为,我最近开始尝试理解Promise及其背后的目的。 While I "think" I understand, I still struggle with how to promisify something to return the future value, then execute a new block of code to do something else. 当我“思考”我理解的时候,我仍然在如何分配一些东西以返回未来值,然后执行一个新的代码块来做其他事情上感到困惑。 Two main node modules I'm using: 我正在使用的两个主要节点模块:

  • pg-promise PG-承诺
  • exceljs exceljs

What I'd like to do is read a file, then once fully read, iterate of each worksheet executing DB commands. 我想做的是读取一个文件,然后完全读取后,迭代执行DB命令的每个工作表。 Then once all worksheets are processed, go back and delete the original file I read. 然后,在处理完所有工作表后,返回并删除我阅读的原始文件。 Here is the code I have. 这是我的代码。 I have it working to the point everything writes into the database just fine, even when there are multiple worksheets . 我有它的工作点,即使有多个工作表,一切都可以很好地写入数据库 What I don't have working is setting it up to identify when all the worksheets have been fully processed, then to go remove the file 我不需要的工作是对其进行设置,以识别所有工作表何时已全部处理完毕,然后删除文件

workbook.csv.readFile(fileName)
            .then(function () {
                // this array I was going to use to somehow populate a true/false array.  
                // Then when done with each sheet, push a true into the array.  
                // When all elements were true could signify all the processing is done... 
                // but have no idea how to utilize this!  
                // So left it in to take up space because wtf...
                var arrWorksheetComplete = [];

                workbook.eachSheet(function (worksheet) {
                    console.log(worksheet.name);
                    db.tx(function (t) {
                        var insertStatements = [];
                        for (var i = 2; i <= worksheet._rows.length; i++) {
                            // here we create a new array from the worksheet, as we need a 0 index based array.
                            // the worksheet values actually begins at element 1.  We will splice to dump the undefined element at index 0.
                            // This will allow the batch promises to work correctly... otherwise everything will be offset by 1
                            var arrValues = Array.from(worksheet.getRow(i).values);
                            arrValues.splice(0, 1);

                            // these queries are upsert.  Inserts will occur first, however if they error on the constraint, an update will occur instead.
                            insertStatements.push(t.one('insert into rq_data' +
                                '(col1, col2, col3) ' +
                                'values($1, $2, $3) ' +
                                'ON CONFLICT ON CONSTRAINT key_constraint DO UPDATE SET ' +
                                '(prodname) = ' +
                                '($3) RETURNING autokey',
                                arrValues));
                        }
                        return t.batch(insertStatements);
                    })
                    .then(function (data) {
                        console.log('Success:', 'Inserted/Updated ' + data.length + ' records');
                    })
                    .catch(function (error) {
                        console.log('ERROR:', error.message || error);
                    });
                });
            });

I would like to be able to say 我想说

.then(function(){
    // everything processed!
    removeFile(fileName)
    // this probably also wouldn't work as by now fileName is out of context?
});

But I'm super confused when having a promise inside a promise.. I have the db.tx call which is essentially a promise nested inside the .eachSheet function. 但是当在promise中有一个promise时,我感到非常困惑。我有db.tx调用,它实际上是嵌套在.eachSheet函数内部的一个promise。 Please help a dumb programmer understand! 请帮助愚蠢的程序员理解! Been beating head against wall for hours on this one. 在这头上被撞了几个小时。 :) :)

If i understand correctly, you're trying to chain promises. 如果我理解正确,则您正在尝试兑现承诺。

I suggest you to read this great article on Promises anti-pattern (see 'The Collection Kerfuffle' section) 我建议您阅读有关Promises反模式的这篇很棒的文章 (请参阅“收集Kerfuffle”部分)

If you need to execute promises in series, this article suggests to use reduce . 如果您需要连续执行Promise,本文建议使用reduce

I'll rewrite your snippet to: 我会将您的代码段重写为:

workbook.csv.readFile(fileName).then(function () {

  processWorksheets().then(function() {
    // all worksheets processed!
  });

});

function processWorksheets() {
    var worksheets = [];

    // first, build an array of worksheet
    workbook.eachSheet(function (worksheet) {
        worksheets.push(worksheet);
    }); 

    // then chain promises using Array.reduce
    return worksheets.reduce(function(promise, item) {
        // promise is the the value previously returned in the last invocation of the callback.
        // item is a worksheet

        // when the previous promise will be resolved, call saveWorksheet on the next worksheet
        return promise.then(function(result) {
            return saveWorksheet(item, result);
        });        

    }, Promise.resolve()); // start chain with a 'fake' promise
}

// this method returns a promise
function saveWorksheet(worksheet, result) {

    return db.tx(function (t) {

      var insertStatements = [];
      for (var i = 2; i <= worksheet._rows.length; i++) {
        // here we create a new array from the worksheet, as we need a 0 index based array.
        // the worksheet values actually begins at element 1.  We will splice to dump the undefined element at index 0.
        // This will allow the batch promises to work correctly... otherwise everything will be offset by 1
        var arrValues = Array.from(worksheet.getRow(i).values);
        arrValues.splice(0, 1);

        // these queries are upsert.  Inserts will occur first, however if they error on the constraint, an update will occur instead.
        insertStatements.push(t.one('insert into rq_data' +
                                    '(col1, col2, col3) ' +
                                    'values($1, $2, $3) ' +
                                    'ON CONFLICT ON CONSTRAINT key_constraint DO UPDATE SET ' +
                                    '(prodname) = ' +
                                    '($3) RETURNING autokey',
                                    arrValues));
      }

      return t.batch(insertStatements);
    })
    // this two below can be removed...
    .then(function (data) {
       return new Promise((resolve, reject) => { 
          console.log('Success:', 'Inserted/Updated ' + data.length + ' records');
          resolve();
       });
    })
    .catch(function (error) {
        return new Promise((resolve, reject) => {
        console.log('ERROR:', error.message || error);
        reject();
      });
    });

}

Don't forget to include the promise module: 不要忘记包括promise模块:

var Promise = require('promise');

I haven't tested my code, could contains some typo errors. 我尚未测试我的代码,可能包含一些拼写错误。

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

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