MaxySpark MaxySpark - 6 months ago 37
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


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