2016-10-04 11 views
0

node.js 코드를 사용하여 A 저장소에서 이미지를 다운로드 한 다음 B 저장소에 업로드하는 기능을 만듭니다. 다른 작업을 계속하기 전에 모든 스트림을 강제로 완료하려고합니다. 나는이 방법을 시도했지만 성공하지 못했습니다. 예 : 실행하면 getImage이 실행됩니다. getImage이 완료되지 않으면 완료 될 때까지 A-> B-> C를 반복합니다. getImage을 완료합니다. 다른 작업을 계속하기 전에 모든 스트림을 강제로 완료하도록하려면 어떻게해야합니까? 나는 A -> B -> C를 실행하기 전에 getImage을 끝내기를 원한다.다른 작업을 계속하기 전에 모든 스트림을 강제로 종료하는 방법은 무엇입니까?

추 신 : pkgCloud를 사용하여 이미지를 IBM Object Storage에 업로드하고 있습니다.

function parseImage(imgUrl){ 
    var loopCondition = true; 
    while(loopCondition){ 
     getImages(imgUrl,imgName); 
     Do task A 
     Do task B 
     Do task C 
    } 
}  

function getImages(imgUrl, imgName) { 
    //Download image from A repository 
    const https = require('https'); 
    var imgSrc; 
    var downloadStream = https.get(imgUrl, function (response) { 

     // Upload image to B repository. 
     var uploadStream = storageClient.upload({container: 'images', remote: imgName}); 
     uploadStream.on('error', function (error) { 
     console.log(error); 
     }); 
     uploadStream.on('success', function (file) { 

     console.log("upload Stream>>>>>>>>>>>>>>>>>Done"); 
     console.log(file.toJSON()); 
     imgSrc = "https://..."; 
     }); 
     response.pipe(uploadStream); 
    }); 
    downloadStream.on('error', function (error) { 
     console.log(error); 
    }); 
    downloadStream.on('finish', function() { 
     console.log("download Stream>>>>>>>>>>>>>>>>>Done"); 
    }); 
    return imgSrc; 
    } 
+0

어떤 함수가'imgSrc'를 정의합니까? 'uploadStream.on ('success''? – guest271314

+0

) ([동기 및 비동기 프로그래밍 (node.js에서)의 차이점은 무엇입니까?] (http://stackoverflow.com/questions/16336367/what-is-the-difference- 사이 동기 및 비동기 프로그래밍 노드에서) – guest271314

답변

0

동기화와 비동기 기능의 차이점을 알고 있어야합니다. getImages 함수는 비동기 코드를 실행하므로이 함수의 결과를 사용하려면 스트리밍이 끝날 때 호출되는 콜백을 전달해야합니다. 다음과 같은 것 :

function parseImage(imgUrl) { 
    getImages(imgUrl, imgName, function (err, imgSrc) { 
     if (imgSrc) { 
     Do task A 
     } else { 
     Do task B 
     } 
    }); 
    } 

    function getImages(imgUrl, imgName, callback) { 
    //Download image from A repository 
    const https = require('https'); 
    var imgSrc; 

    var downloadStream = https.get(imgUrl, function (response) { 
     // Upload image to B repository. 
     var uploadStream = storageClient.upload({ container: 'images', remote: imgName }); 
     uploadStream.on('error', function (error) { 
     console.log(error); 
     return callback(error); 
     }); 

     uploadStream.on('success', function (file) { 
     console.log("upload Stream>>>>>>>>>>>>>>>>>Done"); 
     console.log(file.toJSON()); 
     imgSrc = "https://..."; 

     return callback(null, imgSrc); 
     }); 

     response.pipe(uploadStream); 
    }); 

    downloadStream.on('error', function (error) { 
     console.log(error); 
     return callback(error); 
    }); 

    downloadStream.on('finish', function() { 
     console.log("download Stream>>>>>>>>>>>>>>>>>Done"); 
    }); 
    } 
+0

비동기 및 동기화에 대한 내용을 위의 코드로 이해해 주셔서 감사합니다. 다른 작업을 시작하기 전에 getImage 함수가 완료 될 때까지 코드를 대기 시키려면 어떻게합니까? –

+0

@ ThuậnLê 이미 코드를 리팩토링 했으므로 콜백을 전달하면 트릭을 수행 할 수 있습니다. –

+0

잘못된 정보로 불편을 끼쳐 드려 죄송합니다. 위의 while 루프를 실행 했으므로 실행할 때 getImage가 끝날 때까지 실행됩니다. 기능 –