title: Get files from ftp and show files directory path in console - Node.js
body: <p>I'm new to node.js and trying to get files from FTP with <a href="https://www.npmjs.com/package/ftp" rel="nofollow noreferrer">ftp module</a> and then I want to show files directory path in console. When I run server with npm start, I get all files(thats nice) but console returns empty array. If I run server again with npm start, console show every path(awesome), but not for new files. I have tried to solve my problem with callbacks and setTimeout with no result :C</p>
  
  <pre><code>var Client = require('ftp');
  var c = new Client();
  var path = require('path');
  var fs = require('fs');
  var dir = path.join(__dirname, '../excelTime/');
  var files = fs.readdirSync(dir);
  
  const connectionProperties = {
    host: 'localhost',
    user: 'user_geek',
    port: '21'
  }
  </code></pre>
  
  <p><strong>Get files from ftp</strong></p>
  
  <pre><code>const getFilesFromFTP = () =&gt; {
    c.on('ready', function() {
      c.list('./', function(err, list) {
        if (err) throw err;
        for(var i = 0; i &lt; list.length; i++) {
          (function(i) {
            const name = list[i].name;
            c.get('/' + name, function(err, stream) {
              if (err) console.dir(err);
              if(!err) {
                stream.once('close', function() { c.end();  });
                stream.pipe(fs.createWriteStream('./excelTime/' + name, {flags: 'w'}));
             }
            });
          }).call(this, i);
        }
        c.end();
      });
    });
  
    c.on('error', function(err) {
      console.log(err)
    });
    c.connect(connectionProperties);
  }
  </code></pre>
  
  <p><strong>Get file path</strong></p>
  
  <pre><code>const checkFolder = () =&gt; {
    let promises = files.map(file =&gt; path.join(dir, file));
    Promise.all(promises).then(console.log);
  }
  </code></pre>
  
  <p><strong>Call</strong></p>
  
  <pre><code>setTimeout(function() {
    checkFolder();
  }, 3000);
  
  getFilesFromFTP();
  </code></pre>