Reputation: 11
Not sure if I have got the whole async / await fully understood yet.
My program:
I have two files, (index.js) which I am using an Async function to fire off functions in order, and (ftp-download-and-extract.js) which contains the async function downloadAndExtractNewPriceFiles()
which logs onto an FTP server, downloads the newest files, extracts the zipped files and then returns to index.js the newly created file's locations.
The problem that I am having is that my function downloadAndExtractNewPriceFiles()
is not returning the variable (path_of_extracted_csvs
). No error messages are given.
My guess is that this has something to do with not fully 'getting' async / await.
// index.js - used to process functions in order.
const csvToJson = require('csvtojson');
const ftp_download = require('./src/ftp-download-and-extract.js');
(async () => {
let file_name_array = await ftp_download.downloadAndExtractNewPriceFiles();
console.log(file_name_array);
})();
// ftp-download-and-extract.js - this is the problem function that isn't returning
const decompress = require('decompress');
const path = require('path');
const fs = require('fs');
const ftp = require("basic-ftp");
const unzipped_directory_path = './src/unzipped/';
let file_obj = require('./FTP_dir_details'); // import file names from JSON file
let Catalogue_WriteStream = fs.createWriteStream(__dirname + '/zipped/catalogue.zip');
let Stock_WriteStream = fs.createWriteStream(__dirname + '/zipped/stock.zip');
let Price_WriteStream = fs.createWriteStream(__dirname + '/unzipped/price.csv');
async function downloadAndExtractNewPriceFiles() {
// function downloadAndExtractNewPriceFiles(){
console.log('in downloadAndExtractNewPriceFiles function');
console.log("****************************************");
console.log("** PHASE 1 **");
console.log("** Starting Up **");
console.log("** **");
console.log("****************************************");
console.log("");
let catalogue_csv_path = "";
let price_csv_path = "";
let stock_csv_path = "";
const client = new ftp.Client();
client.ftp.verbose = true;
try {
// Attempt to connect to FTP server
await client.access({
host: "x",
user: "x",
password: "x",
secure: false
});
// **************************************************************
//
// Task 1 - Stock.zip, download and extract
//
// **************************************************************
// Change dir to Catalogue
await client.cd("/Catalogue"); // Catalogue is also known as 'Non-Components'
let remote_catalogue_file = file_obj.catalogue_dir_newest_file;
try {
// Download file
await client.download(Catalogue_WriteStream, remote_catalogue_file);
try {
// UnZip file
// Maybe use this npm module - utility https://www.npmjs.com/package/node-stream-zip
await decompress(Catalogue_WriteStream.path, unzipped_directory_path,).then(files => {
console.log('Unzipped : ' + files[0].path);
console.log('Task 1 Complete')
catalogue_csv_path = files[0].path; // save to variable where CSV extracted file is.
});
} catch (err) {
console.log('Task 1 Failed');
console.log("Error cannot unzip file :" + Catalogue_WriteStream.path);
console.log(err)
}
} catch (err) {
console.log('Task 1 Failed');
console.log("Error cannot download file :" + file_obj.catalogue_dir_newest_file);
console.log(err)
}
// Reset to Root Dir
await client.cd("/");
// **************************************************************
//
// Task 2 - Stock.zip, download and extract
//
// **************************************************************
// Change dir to Stock
await client.cd("/Stock");
let remote_stock_file = file_obj.stock_dir_newest_file;
try {
// Download file
await client.download(Stock_WriteStream, remote_stock_file);
try {
// UnZip file
await decompress(Stock_WriteStream.path, unzipped_directory_path,).then(files => {
console.log('Unzipped : ' + files[0].path);
console.log('Task 2 Complete');
stock_csv_path = files[0].path; // save to variable where extracted CSV file is.
});
} catch (err) {
console.log('Task 2 Failed');
console.log("Error cannot unzip file :" + Stock_WriteStream.path);
console.log(err)
}
} catch (err) {
console.log('Task 2 Failed');
console.log("Error cannot download file :" + file_obj.stock_dir_newest_file);
console.log(err)
}
await client.cd("/");
// **************************************************************
//
// Task 3 - Prices.csv, download and extract
//
// **************************************************************
// IMPORTANT - Price does not need to be decompressed as it is not a ZIP!
// Change dir to Stock
await client.cd("/Price");
let remote_price_file = file_obj.price_dir_newest_file;
try {
// Download file
await client.download(Price_WriteStream, remote_price_file);
} catch (err) {
console.log('Task 3 Failed');
console.log("Error cannot download file :" + file_obj.price_dir_newest_file);
console.log(err)
}
} catch (err) {
console.log("Operation Failed");
console.log("Error : Cannot connect to FTP Server");
console.log(err)
}
client.close()
let new_price_csv_path = path.join(Price_WriteStream.path);
// let new_price_csv_path = path.join(__dirname, 'unzipped', price_csv_path);
let new_stock_csv_path = path.join(__dirname, 'unzipped', stock_csv_path);
let new_catalogue_csv_path = path.join(__dirname, 'unzipped', catalogue_csv_path);
let path_of_extracted_csvs = [new_price_csv_path, new_stock_csv_path, new_catalogue_csv_path];
return path_of_extracted_csvs;
}
module.exports.downloadAndExtractNewPriceFiles = downloadAndExtractNewPriceFiles();
No error messages, just "Process finished with exit code 0", the return from the function is not coming back to index.js. Program is just exiting.
Upvotes: 1
Views: 353
Reputation: 24565
In your module you're not returning the function, instead you're immediately calling it:
module.exports.downloadAndExtractNewPriceFiles = downloadAndExtractNewPriceFiles();
This is why no promise is returned from where you try to wait for it in your index.js file, but instead an UnhandledPromiseRejection
(with the error that downloadAndExtractNewPriceFiles
is not a function) should be thrown causing the script to exit.
So simply returning the function should work:
module.exports.downloadAndExtractNewPriceFiles = downloadAndExtractNewPriceFiles;
Upvotes: 3