javascript - Promise moving onto next .then() before resolving? -
i have simple web app logging onto customer facing webservers , downloading log files on request. when send request route module initiates connects via sftp webserver , downloads logs locally, 2 separate servers. reason last then() method in chain getting triggered before promise resolves.
router.js:
var express = require('express'); var router = express.router(); var gather = require('../api/gather'); /* home page. */ router.get('/', function(req, res, next) { res.render('home'); }); router.get('/api/gather',function(req,res,next){ console.log('sending web9 target'); gather.logs(['web9']) .then((logsfound)=>{ console.log('sending web 11 target'); gather.logs(['web11',logsfound]) }) .then((logsfound)=>{ console.log(logsfound); console.log('downloading finished rendering home message'); res.render('home',{message: logsfound+' log files ready pickup'}); }) .catch((e)=>{ res.render('home',{message: e}); }); }); module.exports = router;
/api/gather.js
//require ftp modules , define client let client = require('ssh2-sftp-client'); let sftp = new client(); var fs = require('fs'); let sftp2 = new client(); //set server configs var boomeconfig = { host: process.env.ftp_host, port: process.env.ftp_port, username: process.env.ftp_user, password: process.env.ftp_pass }; var webserverconfig = { port: process.env.web_port, username: process.env.web_user, password: process.env.web_pass }; let message; const logdir = '/opt/foobar/logs/'; module.exports.logs = function(req, res){ console.log('hit gather module'); const target = req[0]; let logsfound = req[1] || 0; return new promise((resolve,reject)=>{ if(target === 'web9'){ webserverconfig.host = process.env.web9_host; }else if(target === 'web11'){ webserverconfig.host = process.env.web11_host; }else{ message = 'no server target found'; resolve(message); } sftp.connect(webserverconfig) .then(()=>{ return sftp.list(logdir); }) .then((data)=>{ for(var = 0; < data.length; i++){ const remotefilename = logdir + data[i].name; const localfilename = './downloads/'+target+'/' + data[i].name; //console.log('saving file '+localfilename); sftp.get(remotefilename).then((stream) => { stream.pipe(fs.createwritestream(localfilename)); }); } console.log('found '+data.length+' logs on '+target); sftp.end(); logsfound += data.length; console.log('got '+target+' files moving next'); if(target ==='web11'){ console.log('total count '+logsfound); } }).then((logsfound)=>{ resolve(logsfound); }) .catch((e)=>{ console.log('error: '+e); reject(e); }); }) }
my home route getting rendered logsfound undefined.
the thing can think of possible
sftp.get(remotefilename).then((stream) => { stream.pipe(fs.createwritestream(localfilename)); });
is still streaming files when promise resolves.
issue was resolving promises way route written creating promise constructor antipattern. bergi , kevin b.
edited route
var express = require('express'); var router = express.router(); var gather = require('../api/gather'); /* home page. */ router.get('/', function(req, res, next) { res.render('home'); }); router.get('/api/gather',function(req,res,next){ console.log('sending web9 target'); return gather.logs(['web9']) .then((logsfound)=>{ console.log('sending web 11 target'); return gather.logs(['web11',logsfound]) }) .then((logsfound)=>{ console.log(logsfound); console.log('downloading finished rendering home message'); res.render('home',{message: logsfound+' log files ready pickup'}); }) .catch((e)=>{ res.render('home',{message: e}); }); }); module.exports = router;
Comments
Post a Comment