I am facing the challenge of uploading a list of video objects with different attributes and links. Currently, I have set up a loop to go through each object in the list and upload the videos one by one since the service I am using does not support multiple uploads simultaneously. This means that I need to wait for the first video to finish uploading before moving on to the second, and so on.
Below is the code snippet handling this process:
$scope.upload = function(){
$scope.videoList.forEach(function(video){
video.state = "Downloading"
$scope.msg = "The video is downloading"
$http.post("/download",{link : video.link,title : video.title}).then(function(resp){
$scope.msg = "The video has been downloaded on the server, it will now start uploading"
var filen = resp.data["fn"]
var title = resp.data["title"]
video.state = "Uploading"
$http.get("/uploadVid?file=" + filen +"&title=" + title).then(function(resp){
if (resp.data == "0002"){
alert( "You didn't authorize the App yet, please authorize it to be able to use it .")
}
if (resp.data == "000X"){
alert("An error occured, please retry .")
}
else{
$scope.msg = "the video uploaded, here's the link: " + resp.data
video.state = "Done"
video.link = resp.data
}
} )
})
}) }
The issue at hand is that even though the videos should ideally be downloaded and uploaded sequentially, due to the asynchronous nature of the $http
calls, all downloads and uploads occur simultaneously. The loop progresses without waiting for the current iteration to complete, leading to undesired simultaneous actions. I am looking to make this synchronous, where each video is processed one after the other. Ideally, I would prefer not to implement the Promises Interface as I am pressed for time and lack familiarity with it. However, if you are well-versed in Promises, your insights and explanations would be greatly appreciated.