0

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;
  }
  • Which function defines `imgSrc`? `uploadStream.on('success'`? – guest271314 Oct 04 '16 at 04:30
  • See [What is the difference between synchronous and asynchronous programming (in node.js)](http://stackoverflow.com/questions/16336367/what-is-the-difference-between-synchronous-and-asynchronous-programming-in-node) – guest271314 Oct 04 '16 at 04:35

1 Answers1

0

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");
    });
  }
Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
  • Thank you now i understand about Async and Sync. but with my above code How can i make the code wait to getImage function complete before start to do another task? – Thuận Lê Oct 04 '16 at 08:56
  • @ThuậnLê I've already refactored your code, passing a callback will do the trick. – Stavros Zavrakas Oct 04 '16 at 09:01
  • I'm sorry for mis-information. Because i have a while loop above so when run it will run to the end of while then back to run getImage function – Thuận Lê Oct 04 '16 at 10:32