gpt4 book ai didi

javascript - Node JS Promises 然后链接不按顺序

转载 作者:搜寻专家 更新时间:2023-10-31 23:51:25 24 4
gpt4 key购买 nike

我试图让我的 Node JS 代码一步一步地工作,而不是异步地使用 Promise。

我先删除所有电子表格,然后生成它们,然后压缩它们,然后加密压缩包,然后通过电子邮件发送压缩包,然后删除电子表格。

//Schedules the job at a specific time
var start = schedule.scheduleJob({
hour: 19,
minute: 26,
dayOfWeek: 3
}, function() {
sendIt();
});

//Starts the Promise Chain
function sendIt() {
return deleteSpreadsheets().then(generateSpeadsheets).then(zipSpreadsheets).then(encrypt).then(sendEmail).then(deleteSpreadsheets);
}

//Deletes the current Spreadsheets in the folder
function deleteSpreadsheets() {
var promise = new Promise(function(resolve, reject) {
console.log('Deleting Spreadsheets');
var locationSpread = ['Location.xlsx'];

locationSpread.forEach(function(filename) {

if (fs.existsSync("./Spreadsheets/" + filename)) {
fs.unlink("./Spreadsheets/" + filename, (err) => {
if (err) {
console.log('Spreadsheet ' + filename + ' not found');
} else {
console.log('Spreadsheet ' + filename + ' successfully deleted');
}
});
}
});
resolve();
});
return promise;
}

//Generates the new Spreadsheets
function generateSpeadsheets() {

var promise = new Promise(function(resolve, reject) {
console.log('Generating Spreadsheets');

var locationSpread = ['Location.xlsx'];

locationSpread.forEach(function(filename) {

var query = connection.query('SELECT * from ' + filename.slice(0, -5), function(err, rows) {

var workbook = excelbuilder.createWorkbook('./Spreadsheets/', filename);
if (workbook == null) {
console.log('workbook null')
};

var sheet = workbook.createSheet(filename.slice(0, -5), 3, rows.length + 1);
if (sheet == null) {
console.log('sheet null')
};
sheet.set(1, 1, 'First Name');
sheet.set(2, 1, 'Last Name');
sheet.set(3, 1, 'Company');

for (var j = 2, z = 0; z < rows.length; j++, z++) {
sheet.set(1, j, rows[z].firstName);
sheet.set(2, j, rows[z].lastName);
sheet.set(3, j, rows[z].company);
}

workbook.save(function(err) {
console.log('workbook saved ' + (err ? 'failed' : 'ok'));
});
});
});
resolve();
});
return promise;
}

//Generates a Zip file with all the Spreadsheets
function zipSpreadsheets() {

var promise = new Promise(function(resolve, reject) {
console.log('Zipping Spreadsheets');
var zipFolder = require('zip-folder');
zipFolder('./Spreadsheets/', './Spreadsheets.zip', function(err) {
if (err) {
console.log('Failed to zip folders', err);
reject();
} else {
console.log('Successfully zipped folder');
}
});
resolve();
});
return promise;
}

//Encrypts the Spreadsheet
function encrypt() {

var promise = new Promise(function(resolve, reject) {
console.log('Encrypting');
spawn = require('child_process').spawn;
zip = spawn('zip', ['-P', 'something', 'Encrypted.zip', './Spreadsheets.zip']);
zip.on('exit', function(code) {
console.log('Finished encrypting');
resolve();
});
});

return promise;
}

//Sends the Encryped Zip as an attached in an email
function sendEmail() {

var promise = new Promise(function(resolve, reject) {
console.log("MAIL SCHEDULE RUNNING");

var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'email', // Your email id
pass: 'password'
}
});

var content = 'something';

var mailOptions = {
from: 'email', // sender address
to: 'email', // list of receivers
subject: 'Title', // Subject line
text: content,
attachments: [{
// file on disk as an attachment
filename: 'Encrypted.zip',
path: './Encrypted.zip' // stream this file
}]
};

transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
reject();
} else {
console.log('Message sent: ' + info.response);
resolve();
};
});
});
return promise;
}

从日志中可以看出,这似乎没有按计划工作:

Deleting Spreadsheets  
Generating Spreadsheets
Zipping Spreadsheets
Encrypting
**Spreadsheet Location.xlsx successfully deleted**
**Finished encrypting**
MAIL SCHEDULE RUNNING
**Successfully zipped folder**
**workbook saved ok**
Message sent: -----------------------------------------------
Deleting Spreadsheets
Spreadsheet Location.xlsx successfully deleted

它应该是这样的:

Deleting Spreadsheets  
**Spreadsheet Location.xlsx successfully deleted**
Generating Spreadsheets
**workbook saved ok**
Zipping Spreadsheets
**Successfully zipped folder**
Encrypting
**Finished encrypting**
MAIL SCHEDULE RUNNING
Message sent: -----------------------------------------------
Deleting Spreadsheets
Spreadsheet Location.xlsx successfully deleted

最佳答案

您正在返回 promise ,然后以错误的方式解决。此外,您不会等待所有删除发生。检查一下。

function deleteSingle(filename) {
return new Promise((resolve, reject) => {
if (fs.existsSync("./Spreadsheets/" + filename)) {
fs.unlink("./Spreadsheets/" + filename, (err) => {
//handle errors or whatever
return resolve();
});
} else {
//handle if file doesnt exist
return resolve();
}
})
}

//Deletes the current Spreadsheets in the folder
function deleteSpreadsheets() {
//we are maping each value inside locationSpread and passing it to the deleteSingle function which returns a promise
return Promise.all(locationSpread.map(deleteSingle))
}

所以,我拆分了你的代码。 deleteSingle 函数返回一个 promise ,该 promise 在文件被删除后解析(如果文件未被删除或不存在,它也会解析,您可以修改它以执行任何您想要的操作)。

然后,deleteSpreadsheets 函数返回一个 promise,一旦里面的 promise 数组解析完成,它就会被解析。所以你可以调用 deleteSpreadsheets().then.... 等等

关于javascript - Node JS Promises 然后链接不按顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45724618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com