已经下载完文件之后做些什么

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

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,因为这些文件需要不同的时间下载。

谢谢您的帮助。

看看这是否适合你。 只是将请求保存到一个variables,并检查它的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'); }); });