MaxySpark MaxySpark - 20 days ago 6
Node.js Question

callback function executes before the finished request.on function

I made a

npm module
for my personal use to download file
using
request
and displaying the progress bar using
progress


https://github.com/MaxySpark/maxyspark-download

but when I test it the callback function executes first, it should execute after finished the download.

my test file

const maxDonwload = require('maxyspark-download');

var filename = "prog.gif";
var url = "http://skillprogramming.com/images/pictuers/how_many_of_you_get_the_same_feeling.gif";
function endFunc() {
console.log("download completed : "+filename);
}
maxDonwload.download(url,filename,endFunc());


Here is the output

download completed : prog.gif
File Size : 0.50 MB

downloading [====================] 100% 0.0s
HERE HERE


I added a the line
console.log("HERE HRERE");
to node-modules index.js file in

req.on('end' function() {
console.log("HERE HRERE");
callback;
}


enter image description here

console.log("HERE HRERE"); execute after the download complete but callback doesnot

how can i fix this? plz help

i am a beginner

Answer

You are calling the function, instead you should pass it as argument without the ():

maxDonwload.download(url,filename,endFunc);