【发布时间】: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;
}
【问题讨论】:
-
哪个函数定义了
imgSrc?uploadStream.on('success'?
标签: node.js promise ibm-cloud node.js-stream pkgcloud