$q.all will return a resolved promise as long as there is at least one rejected

I am attempting to run multiple promises simultaneously so that I can perform a task once they have all resolved. Even though there is a rejected promise, '$q.all()' still resolves. Am I missing something in the behavior of '$q.all'?

Appreciate any help on this issue!

        function saveOrder () {

            return ordersSrv.saveOrder(order).then(function(data) {

                console.log('saveOrder OK');
            },
            function(error) {

                console.log('saveOrder KO');
            });
        }


        var aPromises = [saveOrder()];

        $q.all(aPromises).then(function () {

            console.log('OK');

        },
        function (error) {
            console.log('---> error');
        });

Answer №1

When you encounter an error in a try catch block and do not rethrow it, the error is considered handled:

try {
   throw Error();
} catch (e) {
    console.log("Error", e);
}
console.log("This statement will also be executed");

The same concept applies to promises:

Promise.reject(Error())
  .catch(e => console.log("Error", e))
  .then(() => console.log("This statement will also be executed"));

If you want to handle the error, make sure to add a catch handler when rejecting with saveError. To log the error but still not handle it, rethrow it:

try {
   throw Error();
} catch (e) {
    console.log("Error", e);
    throw e;
}
console.log("This statement will not be executed");

Similarly with promises:

Promise.reject(Error())
  .catch(e => { console.log("Error", e); throw e; })
  .then(() => console.log("This statement will not be executed"));

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

Tips for concealing an element with Clippy.js without relying on a timeout feature

Recently, I stumbled upon a fascinating JavaScript library called Clippy.js that allows you to incorporate Microsoft Word's classic virtual assistants into your web browser. While experimenting with it, I discovered that the text balloon vanishes afte ...

Ways to verify that a javascript function generates an object and executes a method within that object

Currently, I am in the process of updating server code written in nodejs and incorporating unit tests into the mix. However, I have encountered a challenge that I need assistance with: classX.prototype.methodX = function () { // Create new session ...

Node.js dynamically updates exports for efficient code execution

I am facing an issue with exporting and importing an object in my code. The object starts off empty but gets populated with keys and values when a button is clicked. However, when other files import this object, it still shows as empty. How can I dynamical ...

Which is better for your website: SSG vs SSR?

Currently, I am diving into Nextjs and constructing a website using this framework. The site includes both public pages, protected routes (like user dashboard, user project details, and general user data), as well as product pages. I have been pondering h ...

What is the best way to divide a range of numbers between m and n using JavaScript

Struggling with the Angular slice pipe when working with an observable fetching data. Although not a difficult task, I'm facing confusion. Check out my example below: value$: Observable<number[]>; // Fetches an array like [1, 2, 3, 4, 5, 6, 7, 8 ...

Generate a JSON Object array by collecting data from various elements to make it dynamic

Seeking assistance in creating a dynamic array of JSON objects from the values of various elements. Below are some examples of these elements. There are more elements contributing to the JSON values, but I don't want to repeat the same code three time ...

Challenges Encountered with jQuery/AJAX Form Processing

I am currently facing an issue with two files in my project. The main file, index.php, contains JavaScript code within the head section. Here is the snippet of JavaScript: <script src="http://code.jquery.com/jquery-latest.min.js" type= ...

The issue of resolving NestJs ParseEnumPipe

I'm currently using the NestJs framework (which I absolutely adore) and I need to validate incoming data against an Enum in Typscript. Here's what I have: enum ProductAction { PURCHASE = 'PURCHASE', } @Patch('products/:uuid&apos ...

Transform for loop from Axios post requests to promises

I am currently working with a loop of axios requests: for(const [number, response] of Object.entries(questions)){ axios.post( this.address+'surveypost', {"patientID": patientID, "questionID": number, "lik ...

Sync jQuery resizable resizing

I need assistance with fixing the resizable JQuery feature. I want to be able to resize the parent element and have it automatically resize the children elements inside as well. Below is my current code snippet: <div id="img_holder" style=&quo ...

Encountering a npm error E404 when trying to install unicons package for React development

I recently started working on a weather app project using create-react-app and encountered an issue while trying to install unicons for the project. Despite attempting a few solutions, I was unable to resolve the problem. Here is the command I used for th ...

Controlling dropdown menus filled with AJAX responseData

When it comes to Javascript, JQuery has always been a reliable companion for me. However, there are instances where I encounter challenges that require some extra effort to overcome. Today happens to be one of those days. I've stumbled upon an issue t ...

What could be causing my JavaScript code to not function properly in an HTML document?

Hello, I am a beginner in the world of coding and currently delving into web development. Recently, I was following a tutorial on creating a hamburger menu but I seem to be encountering some issues with the JavaScript code. I have double-checked my Visual ...

Trouble with React routes: only fixed after refreshing the page

import React, { useEffect, useState } from 'react'; import { Container, AppBar, Typography, Grow, Grid, useTheme } from '@material-ui/core'; import { useDispatch } from 'react-redux'; import { BrowserRouter, Router, Route, Swi ...

JavaScript onclick event ceases to function after initial click

I am currently working on designing a custom selection menu using HTML, CSS, and JavaScript. The concept is simple: there is a div element styled to look like an input field that, when clicked, reveals another div containing a list of options. The user can ...

Controlling the display and status of DIV elements as radio button choices within a React application

I am seeking the React.js solution to a common challenge. I have five DIVs that will function as radio button options when clicked. This is what the HTML might look like: //List of option DIVs which act as radio buttons when clicked const options = () = ...

Express.js encountered an unexpected token symbol "<"

I have a basic express server set up like this: Express application: var express = require('express'); var compression = require('compression'); var path = require('path'); var cors = require('cors'); var router = ...

establish a connection with the node server and initiate the automatic mask detection feature

Here's my issue: I have a node server running a game I'm developing. The server listens on an IP address in the network, such as 192.168.x.x (which can vary). The game "frontend" is a web page hosted on a domain (like paperplane.io) that links ...

Processing two Array Objects can be achieved without resorting to the EVAL function

I have two objects that I need to process. obj1 contains an array of objects with formulas. obj2 holds the values needed for the calculations. I am looking for a way to process and calculate both objects in order to obtain a result where the keys present ...

Verifying the presence of an image via its URL may not be functional across all browsers

Hey folks, I'm working on a project that involves displaying an image along with other fields in a loop where the image source changes with each iteration. I also need to check if the image exists and set a default source if it doesn't. The code ...