【问题标题】:How to force all stream done before we continue with other task?在我们继续其他任务之前如何强制所有流完成?
【发布时间】:2017-02-12 03:11:39
【问题描述】:

我正在使用 node.js 代码创建一个函数来从 A 存储库下载图像,然后上传到 B 存储库。我想强制所有流在继续其他任务之前完成。我已经尝试过这种方式,但我没有成功。 示例:当我运行它时,它会运行到 getImage。当getImage没有完成时,会循环A->B->C,直到完成后再完成getImage。在继续执行其他任务之前,如何强制所有流完成?我的意思是我希望在运行 A->B->C 之前完成 getImage

PS:我正在使用 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;
  }

【问题讨论】:

标签: node.js promise ibm-cloud node.js-stream pkgcloud


【解决方案1】:

您应该了解同步和异步功能之间的区别。 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");
    });
  }

【讨论】:

  • 谢谢你,现在我了解了异步和同步。但是用我上面的代码我怎样才能让代码在开始执行另一个任务之前等待 getImage 函数完成?
  • @ThuậnLê 我已经重构了你的代码,通过回调就可以了。
  • 对于错误信息,我深表歉意。因为我上面有一个while循环所以运行时它会运行到while结束然后返回运行getImage函数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-15
  • 2021-01-21
  • 1970-01-01
  • 2014-09-21
  • 2011-03-19
  • 1970-01-01
相关资源
最近更新 更多