Sunday 30 September 2018

Help downloading file and using readline async

I can't get the codes in an async way. Comes up with empty array​app.jsconst argv = require('minimist')(process.argv.slice(2)); const appManager = require('./appManager/appManager.js');start(); function start(){ appManager.start(argv); }​appManager.jsconst doanloadFile = require('../catalog/downloadFile.js'); const fileUtils = require('../utils/fileUtils.js'); const appConfig = require('../config/appConfig.json');async function start(args) { const result = await downloadFile.downloadFile(catalogTxtUrl, filename); console.log('result path: ' + result); const codes = await downloadFile.getCodes(result); console.log(codes); return 'start'; }async function getUrlPath(args) { let urlPath; // do we have file to start with or link if (args.l) { urlPath = args.l; } else { urlPath = await scraper.getLink(appConfig.url); } return urlPath; } module.exports.start = start;downloadFile.jsconst request = require('request'); const readline = require('readline'); const fs = require('fs'); async function downloadFile(urlToFile, filename) { const download = await request({ url: urlToFile, followAllRedirects: true }).pipe(await fs.createWriteStream(filename)); return download.path; };async function getCodes(filename) { return new Promise((resolve, reject) => { const codes = []; const rl = readline.createInterface({ input: fs.createReadStream(filename), crlfDelay: Infinity }); console.log('after const readline' + rl); rl.on('line', (line) => { console.log('line ' + line); const code = reCode.exec(line); if (code) { codes.push(code[0].replace(/ /g, "")); } }); console.log('before close'); rl.on('close', () => { console.log('in close'); resolve(codes); }); }); }module.exports.getCodes = getCodes; module.exports.downloadFile = downloadFile;​

Submitted September 30, 2018 at 01:52PM by jonny_3000

No comments:

Post a Comment