MaxySpark MaxySpark - 1 year ago 75
Node.js Question

callback function executes before the finished request.on function

I made a

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

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 = "";
function endFunc() {
console.log("download completed : "+filename);

Here is the output

download completed : prog.gif
File Size : 0.50 MB

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

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

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

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 Source

You are calling the function, instead you should pass it as argument without the ():,filename,endFunc);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download