Issue with Angular promise finally clause not executing

I am facing an issue with my RestService where the finally clause is not getting called in the controller method, even after assuming it would be triggered on either success or failure. My current setup involves using angular 1.3.13.

var restService = {
'request': function(args) {
    var deferred = $q.defer();
        $http({
            url: url,
            withCredentials: this.use_session,
            method: method.toUpperCase(),
            headers: headers,
            params: params,
            data: data,
            timeout: 20000
        })
        .success(angular.bind(this, function(data, status, headers, config) {
            deferred.resolve(data, status);
        }))
        .error(angular.bind(this, function(data, status, headers, config) {
    deferred.reject(data);
        }
   return deferred.promise;
}

'listAssignments': function(filters) {
  return this.request({
        'method': "GET",
        'url': "/assignments/",
        'params': filters
      });
}

}; // end restService object

 restService.listAssignments(filters)
  .then(function(data) {
    $log.debug("success " + data);  
  }, function(data) {
    $log.debug("error " + data);  
  })
  .finally(function() {
    $log.debug("In finally.... calling scrollHelper");
  });

Answer №1

After spending some time troubleshooting, I finally got it to work. Interestingly, when I added a finally handler to the $http function, the finally handler in the chain was triggered as expected. Here is the code snippet that made it work:

var restService = {
'request': function(args) {
    var deferred = $q.defer();
        $http({
            url: url,
            withCredentials: this.use_session,
            method: method.toUpperCase(),
            headers: headers,
            params: params,
            data: data,
            timeout: 20000
        })
        .success(angular.bind(this, function(data, status, headers, config) {
            deferred.resolve(data, status);
        }))
        .error(angular.bind(this, function(data, status, headers, config) {
           deferred.reject(data);
        })).finally(function() {
           // By adding a finally clause on $http, the finally
           // handler in the chain (used in the controller) gets called.
        });
   return deferred.promise;
}

'listAssignments': function(filters) {
  return this.request({
        'method': "GET",
        'url': "/assignments/",
        'params': filters
      });
}

}; // end restService object

 restService.listAssignments(filters)
  .then(function(data) {
    $log.debug("success " + data);  
  }, function(data) {
    $log.debug("error " + data);  
  })
  .finally(function() {
    $log.debug("In finally.... calling scrollHelper");
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Let's unravel this JavaScript enigma: the code snippet window.confirm = divConfirm(strMessage) awaits

Imagine a scenario where you have an old website with a lot of existing JS code. If a user wants to update all the alert messages to modern, stylish Div-based alerts commonly used in jQuery, YUI, Prototype, etc., there are primarily three types of JS dialo ...

Difficulty encountered when trying to use Bootstrap tooltip with Bootstrap icon

Attempting to implement bootstrap tooltips on bootstrap icons with the following code: <body> <script> const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]') ...

I am looking to incorporate a child row for each tr element within AngularJS

Here is the code snippet I have: <td class="name"> <i class="mdi mdi-plus s16" ng-click="childRowToggle($event)"></i> <div ng-click="details(assets.code)">{{assets.name}}</div> <span class="seco ...

How can I implement a single Ajax call to load content from various pages and display it

This code facilitates an ajax call to dynamically change the content of a div in the main page without requiring a full page reload: function ajaxcall() { $.ajax({ type: "POST", url: "/create.php", success: function( returnedDa ...

Search for and swap out every item in a multi-tiered array

I am working with an array where the first column represents an id: var mainarray = [ ["1001","P6","P8"], ["1002","P7"], ["1003","P7","P8","P10"], ["1004","P6","P10"], ]; My goal is to replace each 'P' element with its corresponding animal from ...

Guide to creating scroll-based animations for a Div element

I've been brainstorming ways to rotate a div as I scroll. My goal is to recreate the effect shown in this codepen. However, I'm struggling to implement scrolling functionality. What I envision is scrolling down causing the word Data to rotate in ...

Disabling Commands using Discord JS Commando in a guild

Curious about something. Will Discord JS Commando disable a command only within a specific server (guild) or globally across all servers? ...

Altering the value of a form upon submission

Struggling with Javascript to update an input value before submitting and storing it in a MySQL table, I encounter the issue of getting a blank row in the database. The code snippet causing this problem looks like this: document.getElementsByName('w ...

Utilize the client-side JavaScript file with ejs framework

Recently, I have been working on creating a website using Express and EJS. I discovered that using just one JavaScript file for all my EJS (view) files was causing issues. If I target a DOM element in one view page and it doesn't exist in another, I w ...

What are some ways I can troubleshoot my contact form?

As someone new to web design, I recently completed my very first site using a combination of HTML, CSS, and a touch of JavaScript. I managed to create a small contact form within the site, but now I'm wondering if there's a way to make it functio ...

Exploration of jsTree capabilities

I am currently exploring the jsTree plugin API and I'm struggling to grasp where exactly the API functions like set_theme, show_dots, etc. should be applied. On this page, I noticed that some functions are preceded by jQuery, while others are precede ...

What is the best way to adjust the value within a span element using plus and minus buttons located near an input field?

Is there a way to implement input addition using + and - buttons, even though it has been working without buttons so far? How can I achieve this functionality with the help of javascript or jquery? // To calculate the sum of three input values in a spa ...

Form submission not being recognized by Ajax's .done() function

I'm trying to include a form from another file using ajax. It's a simple form that is activated by an onclick event defined as follows: <a href="javascript:void(0);" class="remitir" title="Reemitir Solicitud" data-id="'.$value['idso ...

Troubleshooting: Issue with auto height functionality in Angular's ui-grid

Utilizing angular's ui-grid to display records, I have a scenario where one product has 7 entries and another product has 200 entries. By default, the grid is set to display a maximum of 20 rows when there are more than 20 records. If there are less t ...

The offcanvas close button fails to function if initialized through JavaScript

I have integrated offcanvas into the page layout. By default, it remains hidden but I want it to be visible at all times on large screens. On smaller screens, there should be a button to dismiss it, as well as another button in the menu panel to show the o ...

Execute a JavaScript function prior to initiating an AJAX request

To streamline the process of making AJAX calls in my .NET project, I have developed a function called checkConnection that checks for internet connectivity before each call. However, currently, I am manually calling this function on every button click that ...

Transferring data from SocketIO to Vue.js

Is there a way to effectively transfer data results received from socketIO to the Vue instance? Below is my current code implementation: let socket = io(); let users; socket.on('user-connected', (data) => { users = data.count; }); socket ...

"After the document is fully loaded, the Ajax post function is failing to work

I am looking to trigger an Ajax call once my document has finished loading. Here is the code I currently have: <script> $(window).bind("load", function () { getCategories(); }); </script> <script> ...

AngularJS fails to trigger window scroll event

Although I have come across similar inquiries regarding this issue, none of the solutions have proven to be effective for me. Currently, I am working with AngularJS and I am attempting to detect the scroll event. Despite trying numerous variations to capt ...

By default, have jQuery disabled input fields

I am looking to set the inputs to be disabled by default, and I have created a checkbox that will enable them when checked. Here is the code for the checkbox functionality: function toggleStatus() { if ($('#toggleElement').is(':checked&ap ...