我使用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