1

I'm downloading a file in the code bellow...

var download = function(uri, filename, callback, path) {

  request
  .get(uri)
  .on('response', function (response) {
    var responseType = (response.headers['content-type'] || '').split(';')[0].trim();
    var ext = mime.extension(responseType);

    filename += '.' + ext;

    var fileStream = fs.createWriteStream(filename)
                    .on('finish', function () {

                    });

    this.pipe(fileStream);

    path = filename; //this doesn't work, the path by reference gets empty

  });

  //I've tried setting a variable here with the file name's value and 
  //returning it here but it's returning empty value

};

Everything works fine but i need a way to return the filename to the function caller, but it comes empty, i can't set even a variable by reference with this filename value inside the request function.. Could somebody help me? thanks in advance

Junior Silva
  • 387
  • 5
  • 17
  • Definitely it's a duplicate. TL;DR in your code you have an unused callback function, return calculated value throw this function. In other words, your callback function should be called by "download" subroutine when result is ready. – Boris Siscanu Sep 10 '16 at 16:45
  • Thanks guys! It worked ! – Junior Silva Sep 10 '16 at 18:14

0 Answers0