回调函数在完成的 request.on 函数之前执行
callback function executes before the finished request.on function
我做了一个npm module
供我个人使用来下载文件
使用 request
并使用 progress
显示进度条
https://github.com/MaxySpark/maxyspark-download
但是我测试的时候是回调函数先执行,应该是下载完成后执行。
我的测试文件
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());
这是输出
download completed : prog.gif
File Size : 0.50 MB
downloading [====================] 100% 0.0s
HERE HERE
我在
中的节点模块 index.js 文件中添加了行 console.log("HERE HRERE");
req.on('end' function() {
console.log("HERE HRERE");
callback;
}
console.log("HERE HRERE");下载完成后执行但回调没有.
您正在调用 endFunc() 而不是将函数本身作为参数传递。
这应该可以解决问题:
maxDonwload.download(url,filename,endFunc);
您正在调用该函数,您应该将其作为不带 () 的参数传递:
maxDonwload.download(url,filename,endFunc);
我做了一个npm module
供我个人使用来下载文件
使用 request
并使用 progress
https://github.com/MaxySpark/maxyspark-download
但是我测试的时候是回调函数先执行,应该是下载完成后执行。
我的测试文件
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());
这是输出
download completed : prog.gif
File Size : 0.50 MB
downloading [====================] 100% 0.0s
HERE HERE
我在
中的节点模块 index.js 文件中添加了行console.log("HERE HRERE");
req.on('end' function() {
console.log("HERE HRERE");
callback;
}
console.log("HERE HRERE");下载完成后执行但回调没有.
您正在调用 endFunc() 而不是将函数本身作为参数传递。
这应该可以解决问题:
maxDonwload.download(url,filename,endFunc);
您正在调用该函数,您应该将其作为不带 () 的参数传递:
maxDonwload.download(url,filename,endFunc);