Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Really struggling with this at the moment so if anyone had any advice that would be fantastic. Pretty sure it's a case of me over complicating something simple but we'll see!

Using the following to upload files to a server

https://github.com/nervgh/angular-file-upload/wiki/Module-API

I am creating several instances of a file uploader for certain types of files (can't just use one as I need different upload URLs for each)

So I then have a list like

var uploaders = [a list of file object uploaders]

What I want to do is iterate over the list, call uploadAll() on each one, then once the files for each have been uploaded, continue the script.

The problem is I don't think the uploadAll function implements a promise, so when I try the following code the rest of the script continues on before the files have been successfully uploaded.

Heres what I have

var deferred = $q.defer();

var uploaders = [my list of object uploaders]

var allUploads = uploaders.map(function(uploaders) {
    var singleUploadPromise = uploaders.uploadAll();
    return singleUploadPromise;
}); 



$q.all(allUploads).then(function() {    
    console.log('Finished uploading all files')
    deferred.resolve('Finished uploading all files');
}, function(error) {
    deferred.reject(error);
});

return deferred.promise;

The files get uploaded but the rest of the script carries out before they do. When I

console.log(allUploads)

I get a list of undefined items. So clearly I am going wrong here but I am unsure as to how to move forward.

share|improve this question
    
I'm not the best at ES6 but I don't think that uploadAll() returns anything github.com/nervgh/angular-file-upload/blob/master/src/servic‌​es/… – ChrisY Jan 19 '16 at 13:46

Based on looking at the code for the file uploader, .uploadAll() does not return anything, but it does have callback .onCompleteAll(). So, you could create a deffered object that you resolve in the callback function of each uploader's call to uploadAll().

        var uploaders = [my list of object uploaders];

        var allUploads = uploaders.map(function(uploader) {
            // you need one deffered object per uploader
            var deferred = $q.defer();

            // set up an onCompleteAll callback for each uploader
            uploader.onCompleteAll = function() {
                deferred.resolve('onCompleteAll');
            };

            // call uploadAll on each uploader
            uploader.uploadAll();

            return deferred;
    }); 


    var combinedUploads = $q.all(allUploads).then(function() {    
            console.log('Finished uploading all files');
    }, function(error) {
            console.log('error!');
    });

    return combinedUploads.promise; // assuming this was in another function
share|improve this answer
    
Thanks for posting, for sure looks like a possible solution. Will let you know how I go when I get a chance! – user2085143 Jan 21 '16 at 17:59
    
Great! I'm very curious to know if that was what you needed. I can tinker with it some more if that doesn't work. – Shelly Coburn Jan 22 '16 at 5:04

it`s the uploadAll() method that is causing your problem i guess.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.