Search code examples
node.jspromiseibm-cloudnode.js-streampkgcloud

How to force all stream done before we continue with other task?


I'm using node.js code to create a function to download an image from A repository and then upload to B repository. I want to force all streams to complete before it continues with other tasks. I have tried this way, but I have not been successful. Example: When I run it, it will run into getImage. When getImage is not completed, it will loop through A->B->C until they are complete and then it completes getImage. How can I force all streams to complete before it continues with other tasks? I mean I want getImage to be finished before running A->B->C.

PS: I am using pkgCloud to upload the image to 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;
  }

Solution

  • You should understand the difference between sync and async function. The getImages function is executing async code and therefore if you want to use the results of this function you have to pass a callback that will be called when the streaming will finish. Something like that:

      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");
        });
      }