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