Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular async function doesn't wait to be finished before continuing [duplicate]

I have an upload function which loops through the selected files and adds them on the servers file system.

Upload function:

$scope.uploadImages = function () {
        for (var i = 0; i < $scope.imageModels.length; i++) {
            var $file = $scope.imageModels[i].file;
            (function (index) {
                $upload
                    .upload({
                        url: "/api/upload/",
                        method: "POST",
                        data: { type: 'img', object: 'ship' },
                        file: $file
                    })
                    .progress(function (evt) {
                        $scope.imageProgress[index] = parseInt(100.0 * evt.loaded / evt.total);
                    })
                    .success(function (data) {
                        $scope.imageProgressbar[index] = 'success';

                        // Add returned file data to model
                        $scope.imageModels[index].Path = data.Path;
                        $scope.imageModels[index].FileType = data.FileType;
                        $scope.imageModels[index].FileSize = $scope.imageModels[index].file.size;

                        var image = {
                            Path: data.Path,
                            Description: $scope.imageModels[index].Description,
                            Photographer: $scope.imageModels[index].Photographer
                        };
                        $scope.images.push(image);
                    })
                    .error(function (data) {
                        $scope.imageProgressbar[index] = 'danger';
                        $scope.imageProgress[index] = 'Upload failed';

                        alert("error: " + data.ExceptionMessage);
                    });
            })(i);
        }
        return $scope.images;
    }
};

If I call this alone it works just fine, but when I put it together with my other functions it seems like it doesn't finish:

 $scope.create = function () {
    $scope.ship = {};

    // This function is asynchronous
    $scope.ship.Images = $scope.uploadImages();

    // Here $scope.ship don't contain any Images
    angular.extend($scope.ship, $scope.shipDetails);

    shipFactory.createShip($scope.ship).success(successPostCallback).error(errorCallback);
};

$scope.ship contains no images, when I debug it starts uploading them, but doesn't wait for it to complete and just executes the next lines of code.

How can I make it work so I make sure the $scope.uploadImages function is done before it continues?

like image 932
Lars Avatar asked Sep 27 '26 17:09

Lars


1 Answers

In order to be sure that your function is done you need to use promises.

Here is a very useful video tutorial that explains how to use them:

https://egghead.io/lessons/angularjs-promises

like image 193
Tome Pejoski Avatar answered Sep 29 '26 12:09

Tome Pejoski