Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 下载文件后执行某些操作_Javascript_Node.js - Fatal编程技术网

Javascript 下载文件后执行某些操作

Javascript 下载文件后执行某些操作,javascript,node.js,Javascript,Node.js,一般来说,我对编程有点陌生。我的问题是我想下载一个文件,然后做一些事情 Danbooru.search(tag, function (err, data) { //search for a random image with the given tag data.random() //selects a random image with the given tag .getLarge() //get's a link to the image .pip

一般来说,我对编程有点陌生。我的问题是我想下载一个文件,然后做一些事情

Danbooru.search(tag, function (err, data) { //search for a random image with the given tag
    data.random() //selects a random image with the given tag
        .getLarge()  //get's a link to the image
        .pipe(require('fs').createWriteStream('random.jpg')); //downloads the image
    });
现在,我想在下载文件后执行console.log。我不想使用setTimeout,因为下载文件需要不同的时间


谢谢你的帮助。

看看这是否适合你。只需将请求保存到一个变量,并检查其上的
finish
事件

Danbooru.search(tag, function (err, data) { 
        var stream = data.random() 
            .getLarge() 
            .pipe(require('fs').createWriteStream('random.jpg'));

            stream.on('finish', function() { console.log('file downloaded'); });
        });