2016-10-28 2 views
1

나는 프로그래밍에 대해 약간 새로운 지식을 가지고 있습니다. 내 문제는 내가 파일을 다운로드하고 그 후에 뭔가를하고 싶다는 것이다.파일을 다운로드 한 후 무언가를 수행

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을 사용하고 싶지 않습니다.

도움 주셔서 감사합니다.

답변

1

이 방법이 적합한 지 확인하십시오. 그냥 변수에 요청을 저장하고 그것에 대한 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'); }); 
     }); 
관련 문제