回调函数在完成的request.on函数之前执行

时间:2016-11-16 15:56:36

标签: javascript node.js npm callback node-modules

我为了个人用途下载了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

我在

中的node-modules index.js文件中添加了一行console.log("HERE HRERE");
req.on('end' function() {
    console.log("HERE HRERE");
    callback;
}

enter image description here

console.log(" HERE HRERE");在下载完成后执行,但回调不是

2 个答案:

答案 0 :(得分:3)

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

maxDonwload.download(url,filename,endFunc);

答案 1 :(得分:1)

您正在调用endFunc(),而不是将函数本身作为参数传递。

这应该解决它:

System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);  // to disable marionette, by default true
WebDriver driver = new FirefoxDriver(d);