【发布时间】:2017-02-25 11:55:33
【问题描述】:
因此,由于 javascript 的异步行为,我最近深入研究了 Promise 及其背后的目的。虽然我“认为”我理解,但我仍然在努力解决如何承诺某些东西以返回未来值,然后执行一个新的代码块来做其他事情。我正在使用的两个主要节点模块:
- pg-promise
- exceljs
我想做的是读取一个文件,然后在完全读取后,迭代执行 DB 命令的每个工作表。然后在处理完所有工作表后,返回并删除我阅读的原始文件。这是我的代码。 即使有多个工作表,我也可以将所有内容都写入数据库。我没有工作的是设置它以识别所有工作表何时已完全处理,然后删除文件
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);
});
});
});
我想说
.then(function(){
// everything processed!
removeFile(fileName)
// this probably also wouldn't work as by now fileName is out of context?
});
但是当在 promise 中包含 promise 时,我感到非常困惑。我有 db.tx 调用,它本质上是嵌套在 .eachSheet 函数中的 promise。 请帮助愚蠢的程序员理解!在这个上已经打了好几个小时了。 :)
【问题讨论】:
-
你需要承诺
workbook.eachSheet并链接它。您应该将db.tx移到eachSheet调用之外,因为您只需要一个数据库事务。 -
Promisify 你所有的异步操作,然后当你嵌套它们时,返回一个链接它们的承诺。不要将常规异步回调与 Promise 混合使用。
-
@vitaly-t 一直在环顾四周,我很难理解如何“承诺” eachsheet 函数。有没有例子你也可以指点我?
-
您可以通过结合使用 Promise 和 ES6 生成器来解决这个问题 - 是否有任何环境限制阻止您这样做?
标签: javascript node.js excel bluebird pg-promise