我使用node.js代码来创建一个函数,从存储库下载图像,然后上传到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''? – guest271314
请参阅[同步和异步编程有什么区别(在node.js中)](http://stackoverflow.com/questions/16336367/what-is-the-difference- – guest271314