2

I am trying to detect if the foreach statement is completed together with the statements inside the foreach. From my research, many are asking to use promises. As I am implemented it, it is not executing the way I was expecting.

var uploadUrl = "/api/upload";
$('#add_product').closeModal();
 var promises = angular.forEach(vm.images_selected, function(value , key){
    return File_Upload.uploadFileToUrl(value, uploadUrl)
    .success(function(result){
        vm.images_selected_uploaded.push(result);
        console.log('here')
    });
})
$q.all(promises).then(function () {
    console.log('there')
    console.log(vm.images_selected_uploaded)
})

From the code above (let's say the value length is 2, the output will be

there
here
here

What I really wanted was here here there. What am I missing here?

2
  • You should move success to then in $q.all(). Commented Dec 9, 2015 at 9:24
  • 2
    .forEach should be .map (assuming angular.forEach/angular.map are similar functionally to array.prototype.forEach/array.prototype.map ... hmmm, no angular.map ... Commented Dec 9, 2015 at 9:25

2 Answers 2

6

First problem, angular.forEach returns a reference to the first argument, so they wont be the promises you're looking for

Second problem, angular doesn't have a drop in replacement for .map

If you save the promises in the promises array, you can do this

var uploadUrl = "/api/upload";
$('#add_product').closeModal();
var promises = [];
angular.forEach(vm.images_selected, function(value , key){
    promises.push(File_Upload.uploadFileToUrl(value, uploadUrl)
        .success(function(result){
            vm.images_selected_uploaded.push(result);
            console.log('here')
        })
    );
})
$q.all(promises).then(function () {
    console.log('there')
    console.log(vm.images_selected_uploaded)
});
Sign up to request clarification or add additional context in comments.

Comments

2

Something like this:

var uploadUrl = "/api/upload";
$('#add_product').closeModal();
var promises = [];
angular.forEach(vm.images_selected, function(value , key){
    promises.push(File_Upload.uploadFileToUrl(value, uploadUrl));
});
$q.all(promises).then(function (results) {
    for(var i = 0; i < results.length; i++){
       var result = results[i];
       vm.images_selected_uploaded.push(result);
       console.log('here');
    }
    console.log('there');
    console.log(vm.images_selected_uploaded);
})

OR Perhaps like this:

var uploadUrl = "/api/upload";
$('#add_product').closeModal();
 var promises = [];
 angular.forEach(vm.images_selected, function(value , key){
    var promise = File_Upload.uploadFileToUrl(value, uploadUrl);
    promise.success(function(result){
        vm.images_selected_uploaded.push(result);
        console.log('here');
    });
    promises.push(promise);
})
$q.all(promises).then(function () {
    console.log('there');
    console.log(vm.images_selected_uploaded);
})

Both is untested btw.

10 Comments

I think I have understand it. Thanks Arg0n! Your solution works like a charm!
And maybe more what you're aiming for.. This way they can finish in any order and you will get "here" in console before all is completed. The first is used more when you need all the results completed in order to do something.
You are right, I will use the first because i need the results to be completed. Thanks
this doesn't look like it changes anything ... promises is the original vm.images_selected array, not an array of promises - unless angular documentation lies - but, it's apparently worked, so what do I know
@JaromandaX Could you provide a link to what you are reading?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.