gahhon
New Coder
index.js
utils.js
Result

Problem:-
How can I return the filteredFile to index.js after download complete for each file or how to know when the downloading is totally complete?
I dig and tried a lot of google research, non-of them working for my current situation tho.
JavaScript:
const Utils= require('./utils');
async function callFTP() {
const responseFTP = await Utils.FtpAPI('url', 'username', 'password', '202211070859.xml')
console.log('index: ', responseFTP);
}
callFTP();
utils.js
JavaScript:
const convert = require('xml-js');
const moment = require('moment');
var PromiseFtp = require('promise-ftp');
const fs = require('fs');
async function FtpAPI(ftpURL, ftpUserName, ftpPassword, ftpLastFileRead) {
//Initialize Promise FTP Connection
const promiseFtp = new PromiseFtp();
const remoteFilePath = '/BR/AGIN/20221107';
const localTempPath = './public/temp/';
const connectionProp = {
host: ftpURL,
user: ftpUserName,
password: ftpPassword
};
return new Promise((resolve, reject) => {
promiseFtp.connect(connectionProp).then(() => {
return promiseFtp.list(remoteFilePath);
}).then((data) => {
let filteredFile = [];
//Filter out already read files
if (ftpLastFileRead != null) {
let tempList = data.filter((element) => {
return element.name > ftpLastFileRead;
});
//Store into filteredFile Object
tempList.forEach((fileElement) => {
filteredFile.push({
fileName: fileElement.name,
fileDate: fileElement.date
});
});
} else {
data.forEach((fileElement) => {
filteredFile.push({
fileName: fileElement.name,
fileDate: fileElement.date
});
});
}
// //filteredFile Iteration to download files to local repository
filteredFile.forEach((listFile) => {
promiseFtp.get(`${remoteFilePath}/${listFile.fileName}`).then((stream) => {
stream.pipe(fs.createWriteStream(localTempPath + listFile.fileName));
console.log(`[game-utils.js] ### ${localTempPath}${listFile.fileName} FTP-API is Downloaded`);
});
});
resolve(filteredFile); //Trying to return JSON Array after downloaded
}).then(() => {
promiseFtp.end();
}).catch((error) => {
promiseFtp.end();
console.log('Exception / Error: ' + error);
reject(error);
})
});
}
module.exports = {
FtpAPI
};
Result

Problem:-
How can I return the filteredFile to index.js after download complete for each file or how to know when the downloading is totally complete?
I dig and tried a lot of google research, non-of them working for my current situation tho.