0

I have some functions in Angularjs that carry a bit of business, also called services for obtaining data, how can I make my code go synchronously (that I hope my first function ends to execute the next?)

angularJs

    $scope.modalAction = function () {
         $scope.UploadFile(),
         $http.post(UriTempV2 + "/Template/InsertTemplate?OwnerRefId=" + $scope.OwnerRefId, $scope.newItem)
           .then(function (response) {
                   //llenar newTemplate
                  CloseModal();    
           }, function (error) {
             alert("Ocurrio un Error!!");
         });        
    }
};
$scope.UploadFile = function () {
    if (document.getElementById('file').files.length !== 0) {
        var file = document.getElementById('file').files[0];
        var uploadUrl = UriTempV2 + "/Template/TemplateFile";
        var fd = new FormData();
        fd.append('file', file);
        fd.append('systemName', $scope.newItem.templateFile);
        fd.append('instanceId', '698757BA-3D34-461D-A8FA-2D7E6BCDDC3C');
        fd.append('enviroment', '90B28DB1-EE45-4F85-A1D4-0C0706737607');
        fd.append('metaData', '{}');

        $http({
            url: uploadUrl,
            method: "POST",
            data: fd,
            headers: { 'Content-Type': undefined }
        }).then(function (response) {
            if (response.status === 200) {
                $scope.TemplateFile = response.data;
                $scope.newItem.templateFile = $scope.TemplateFile.SystemName;
                $scope.newItem.outputOriginalName = $scope.TemplateFile.OriginalName;
                if ($scope.newItem.outPutType != 'bodyMail') {
                    $scope.CreateFields();
                }
            } else {
                alert("Ocurrio un Error!!");
            }
        }, function (error) {
            alert("Ocurrio un Error!!");
        });
    }

    var templateFile = $scope.newItem.templateFile;
    return templateFile
};
$scope.CreateFields = function () {
    var uploadUrl = UriTempV2 + "/Template/Sync/" + $scope.newItem.id + "/" + $scope.newItem.templateFile;
    $http.get(uploadUrl);
};

The idea is that the UploadFile function is executed, within this function CreateFields and at the end of these, the service "/ Template / InsertTemplate? OwnerRefId =" is executed, when the latter is finished, the CloseModal function will be executed.

Community
  • 1
  • 1
Led Tapgar
  • 41
  • 5
  • The AngularJS framework does not support synchronous requests. In addition [many browsers have deprecated synchronous requests](https://stackoverflow.com/a/31097882/5535245) on the main thread due to the negative effects to the user experience. Instead use [promise chaining](https://docs.angularjs.org/api/ng/service/$q#chaining-promises) to create sequential XHRs. – georgeawg Sep 30 '19 at 21:47

1 Answers1

0

The AngularJS framework does not support synchronous requests. In addition many browsers have deprecated synchronous requests on the main thread due to the negative effects to the user experience. Instead use promise chaining to create sequential XHRs.

From the Docs:

Synchronous request

Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), Blink 39.0, and Edge 13, synchronous requests on the main thread have been deprecated due to their negative impact on the user experience.

Synchronous XHR requests often cause hangs on the web. But developers typically don't notice the problem because the hang only manifests with poor network conditions or when the remote server is slow to respond. Synchronous XHR is now in deprecation state. The recommendation is that developers move away from the synchronous API and instead use asynchronous requests.

For more information, see

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95