When cascading, $q.All did not wait for all promises to finish

Working with $q.all to execute an array of promises asynchronously has been my recent task. I encountered a situation where I needed to ensure that one of the elements in the array completes its promise execution before moving on to the outermost 'then' function. Here's a snippet of the code:

$q.all([
        function1().then(function () { }, function (reason) { addToErrorList($scope, reason) }),
        function2().then(function () { }, function (reason) { addToErrorList($scope, reason) }),
        function3().then(function () { }, function (reason) { addToErrorList($scope, reason) }),
        function4().then(function () { }, function (reason) { addToErrorList($scope, reason) }),
        function5($q).then(
            function() {
                $q.all([
                    function51($q).then(function () { }, function (reason) { addToErrorList($scope, reason) }),
                    function52($q).then(function () { 
                        someLogic();
                    }, function (reason) { addToErrorList($scope, reason); }),
                ])})
    ]).then(function () {
        usSpinnerService.stop('spinner');

        if ($scope.errorList.length > 0) {
            showMessages($scope, $scope.errorList, "error");
        }
    });

It appears that the code proceeds to execute "usSpinnerService.stop('spinner');" before completing the tasks in function51 and function52. I am looking for a way to ensure that all functions within the outer $q.all are executed before proceeding. Any suggestions would be appreciated. Thank you.

Additionally, I noticed that the calls to addToErrorList($scope, reason) in the first four functions are executed asynchronously. Is there a way to make them run synchronously before moving on to the outer 'then' function?

Correction: The above paragraph is incorrect. Upon further inspection, I confirmed that addToErrorList does run before reaching the outer 'then' function.

Answer №1

To solve this issue, make sure to include $q.all like this:

function() {
    return $q.all([
        function51($q).then(function () { }, function (reason) { addToErrorList($scope, reason) }),
        function52($q).then(function () {
            performActions($http, $scope, $q, sharedService);
        }, function (reason) { addToErrorList($scope, reason); }),
    ])})

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

Issue with authentication when accessing Google Video API

I'm attempting to utilize the Google API provided: I have downloaded the Sample Project and followed these steps: 1) Navigate to the Project Folder named API Video 2) Run npm install 3) Set GCLOUD_PROJECT = neorisvideo 4) Download Json from the C ...

Interactive YouTube Video Player that keeps video playing in original spot upon clicking the button

Currently, I'm working on a project that involves combining navigation and a video player within the same div container. Here is an image link And another image link The concept is that when you click on one of the four boxes, a new video will appe ...

What is the best way to transform this unfamiliar CSS element into JavaScript code?

I'm facing an issue where I want to animate a CSS image based on time constraints, but since it's in CSS, I'm unable to achieve the desired effect. The animation in question is not the sun itself, but rather a yellowish half-circle animation ...

Using the ng-click directive with checkboxes in Angular

Angular 1.2: <input type="checkbox" ng-model="vm.myChkModel" ng-click="vm.myClick(vm.myChkModel)"> The myClick function doesn't have the correct state? I'm trying to get the updated state after clicking. ...

When selecting an old list, only the most recently created ID is displayed, rather than the ID of the specific list being

I've set up a dashboard where I can create lists and have them displayed on the screen, but there seems to be an issue when trying to open any list as only the last created one opens. Can anyone point out what mistake I might be making? The technologi ...

Require directive in Angular must be placed on the same element

Hello, I am attempting to require on the same element. Angular documentation states that this is possible. By using a ^ prefix, the directive will search for the controller on its own element or its parents; without any prefix, the directive would onl ...

The AJAX request was successful, however, the PHP script did not return any

When using an Ajax success function to alert data in JavaScript, there may be occasions where the PHP side shows that the GET array is empty. <script type="text/javascript"> var myArray={ name:"amr", age:22 } myArray =JSON.stringify(myA ...

The functionality of two-way data binding seems to be failing when it comes to interacting with Knock

I am currently working on a piece of code that consists of two main functions. When 'Add more' is clicked, a new value is added to the observable array and a new text box is displayed on the UI. Upon clicking Save, the values in the text boxes ...

Getting access to the DOM element in AngularJS can be achieved through various methods

I'm a newcomer to AngularJS and I have successfully set up basic routing in my app. However, I am now looking to enhance the interactivity of my home screen by incorporating a typewriter js script: const texts = ['123', '456', &ap ...

Seeking assistance with formatting output for OpenCPU and igraph

Here is my adjacency array data: var g = [[10, 2], [15, 0], [18, 3], [19, 6], [20, 8.5], [25, 10], [30, 9], [35, 8], [40, 5], [45, 6], [50, 2.5]] The code I am using in OpenCPU is as follows: ocpu.call("centralization.closeness", {graph: g} ...

Is there a directive in AngularJS that allows for binding HTML templates using ng-bind-html

I'm working on a directive that has the ability to use dynamic templates with expressions inside them. The challenge I face is if I use ng-bind-html, the expression won't be evaluated properly. On the other hand, using ng-bin-template results in ...

What is the best way to define a dynamic model name in AngularJS?

I am looking to dynamically populate a form with custom questions (check out the example here): <div ng-app ng-controller="QuestionController"> <ul ng-repeat="q in Questions"> <li> <div>{{q.Text}}</div> ...

Acquiring information from a different Vue.js component

I am faced with a puzzle involving 2 components, A and B. Component A: import B from '../components/B.vue'; export default { components: { B }, methods: { test: function() { console.log(B.data().settin ...

What is the best way to position two elements floated to the right in separate rows within a single row?

Having trouble with a web design issue. I am currently working on a website and trying to format an input field with a label so that the label sits on top of the input while floating to the right. However, I'm encountering difficulties as the elements ...

Navigating between views in AngularJS using $routeProvider and relative URLs in an ASP.NET MVC

I am completely new to AngularJS and I wanted to experiment with relative URLs using $routeProvider. Let's consider the following scenario: I have an "Edit" page, the link for the ASP.NET MVC page would be: http://localhost/Workflow/Edit Therefore ...

Having trouble loading HDRI image as environment using Three.js

I recently started using Three.js and am currently tackling a project that involves utilizing an HDRI file for environmental mapping for lighting and reflection purposes. I've been experimenting with loading HDRI files using the HDRCubeTextureLoader a ...

Checking for the winner in a JavaScript tic-tac-toe game

Here is the code snippet for a tic-tac-toe game: $(".square").one("click", function() { if( gameOver == false ) { sq1 = $("#sq1").text(); // captures the value of squares after being clicked. sq2 = $("#sq2").text(); //and so on for all 9 squares } / ...

Guide to extracting a specific JSON object value using the $http service in AngularJS

Is there a way to extract a specific value from a JSON object and store it in a variable? In this case, I am looking to assign the 'languages' value to an array variable. Can anyone guide me on how to achieve this? $http.get("http://localhost:81 ...

A guide on how to cycle through and modify key-value pairs using a Patch Request in MongoDB

I am working on configuring a patch request for the endpoint "/api/user?username=idHere". This patch request should accept a JSON body and update the user in MongoDB with the new key-value pairs. Currently, the line "{$set: {key: req.body[ ...

To address the issue of input values not persisting in a Selenium iframe element, I am implementing a custom JavaScript event to

Attempting to initiate a JavaScript 'change' event on an element within an iframe. The following is the code snippet I have been using: // accessing the iframe window var iframeDoc = document.getElementsByTagName("iframe")[0].contentWin ...