mscdex / node-ftp

An FTP client module for node.js
MIT License
1.13k stars 244 forks source link

Add Promise versions of methods #285

Open SeriousMonk opened 1 year ago

SeriousMonk commented 1 year ago

Hi,

I am currently trying to use this package to upload an array of files to an ftp server. Currently since the methods only support callbacks, this is impossible if not by writing some horrendous code.

It would be very neat to have promise versions of all methods so that I could write code like this:

const c = new Client();

c.on('ready', async function(){

  for(let f of files){
    try{
      await c.put(`tmp/${req.body.user._id}/${f}`, `images/user/${req.body.user._id}/${f}`);
      fs.unlinkSync(`tmp/${req.body.user._id}/${f}`);
    }catch(e){
      files.splice(files.indexOf(f), 1); //remove file from files array
    }
  }

  c.end();

  return res.status(200).send({success: true, paths: files});
});

c.connect(JSON.parse(process.env.FTP_CONFIG));