async js callback for each upload

105 views Asked by At

I'm new at web develop. I was faced with the following problem:

I upload images at amazon s3 and getting callback with response which contains link to this image. Then I push it into array and store this array in DB. But I don't know how to go to the next function exactly when all images are uploaded.

In this chunk i'm going to the next function in waterfall when the length of count array number of downloaded images in the array equal to the number of links received in response. but it works for some reason, only an odd number of downloaded images.

How can you do so that you can move on to the next function in the falls only after I get all

$scope.createPromo = function () {
    async.waterfall([
        function(callback) {
            var promImageLoc = [];
            var upload = $scope.uploaderPromo.queue;
            $scope.uploaderPromo.uploadAll();
            $scope.uploaderPromo.onSuccessItem = function(fileItem, response, status, headers) {
                console.log('success response', response);
                    promImageLoc.push(response.location);
                    if (upload.length === promImageLoc.length) {
                    callback(null, promImageLoc);
                }
            }
        },
1

There are 1 answers

1
Tanase Butcaru On BEST ANSWER

I never used async-waterfall, but if I had to do this I would use $q promises and when the resolve event is received, then you can call your desired function.

Start here.

Also, lookup for $q.all() - that's what will help you.
Here's a $q.all() example with success event (when all http requests are done): Is there a second success in angular $q.all() as in jQuery $.get()