简体   繁体   中英

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;
}

在此处输入图片说明

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

您正在调用该函数,而应将其作为不带()的参数传递:

maxDonwload.download(url,filename,endFunc);

You are calling endFunc() instead of passing the function itself as a parameter.

This should fix it:

maxDonwload.download(url,filename,endFunc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM