What is causing the rejection to stay suppressed?

I noticed that when function uploadLogs() is rejected, the expected rejection bubbling up to be handled by function reject(reason) does not occur. Why is this happening?

In the code below, a rejection handler for function uploadLogs() successfully handles the rejection:

    return EventCollector.persist().then(function persistResolve() {
        return EventCollector.uploadLogs().then(function uploadLogsResolve() {
            return closeApp();
        }, function rejectionHandler() {
            console.log("this rejection handler manages the event")
        });
    }, function reject(reason) {
        return closeApp();
    });

However, if I remove the rejection handler and expect the rejection to bubble up and be handled by the rejection handler of persist(), it surprisingly doesn't.

    return EventCollector.persist().then(function persistResolve() {
        return EventCollector.uploadLogs().then(function uploadLogsResolve() {
            return closeApp();
        });
    }, function reject(reason) {
        console.log("rejection is not handled when uploadLogs() fails");
        return closeApp();
    });

Shouldn't promise chaining and rejection bubbling operate in this manner as intended?

Answer №1

Handlers for success or rejection are executed based on whether the promise is resolved successfully. The children handlers are called within the success functions, indicating that the parent function has already been determined successful at that point. It wouldn't make sense to suddenly fail the parent function just because a child operation fails.

In essence, the behavior with promises should not be expected to mimic a synchronous setup. The example provided illustrates how unexpected outcomes can occur when trying to handle promises in a synchronous manner.

if( persist() ) {
  if( upload() ) {
    closeApp();
  }
  else {
    console.log('This would obviously not go into the below else for persist().');
  }
}
else {
  closeApp();
}

This demonstrates how promises simulate behavior asynchronously rather than synchronously.

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

Sending a parameter to a different function (on a separate webpage)

At the start of my webpage, there are two radio buttons on the first page, each with its own value. Upon clicking a link to move to the second page, a for loop is activated to grab the value of the selected button. The script has been tested and works as e ...

Retrieve only the initial tag content using jquery

My goal is to extract the "22" from the following code... <div class="left"> <a class="count-link" href="http://url1.com"> <span>22</span> users </a> <a class="count-link" href="http://url2.com"> <span>10</span ...

A JavaScript function to separate a URL and eliminate the final segment

http://www.google.com/site!#656126.72367 Is there a way to split and remove the part after the exclamation mark in this URL using JavaScript when the page is loaded? I am looking to achieve http://www.google.com/site ...

Adjust the CSS of a dynamically generated jQuery checkbox button in real-time

I am working on a project where I am creating a series of jQuery checkboxes dynamically within a loop. Here is how I am doing it: var checkbox = $('<input>').attr({type: 'checkbox', id: checkbox_id); panel.append(checkbox); panel ...

Transforming an Ext.data.TreeStore data structure into a JSON format

How can I convert an Ext.data.TreeStore to a string for saving to Local Storage? I tried using Ext.encode() but it's giving me a circular structure error. Has anyone encountered this issue and found a workaround? ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

How can you use ng-click to re-sort data that has already been loaded using Angular's ng-click?

I'm having trouble switching between loading and sorting the information in the table using ng-click. The functions to load and sort work correctly individually, but I can't seem to switch between the two. It seems like I reset the countries data ...

JavaScript code to access values from a JSON object

{ "4": { "structure": "Archaeological Museum", "boxes": [{ "id": "5", "name": "Ground Cassa DEMO" }] }, "5": { "structure": ...

AngularJS is not displaying the full value of the model in the user interface

I have a technique for saving an object into an array after modifying some of its properties. However, I am facing an issue where not all the modified properties reflect in the user interface. Code : HomeController.js $scope.MainArray=[]; $scope.newIt ...

Encountered a 404 error when attempting to deploy create-react-app due to failed resource loading

Any assistance would be greatly appreciated. Thank you! Although my website runs smoothly locally, I am encountering issues when trying to deploy it in a production environment. Upon executing npm run deploy, the expected outcome is an automatic build for ...

JavaScript prototype error: caught TypeError - attempting to call undefined function

I am facing challenges while attempting to create a Factory in AngularJS. I have moved the code from the controller to the factory and made a few adjustments for it to function properly. Here is the error that I am encountering: "El objeto no acepta la p ...

Assign a value to the <li> element and modify the prop when the user clicks using vue.js

I've been attempting to update props from child to parent using the $event in an @click event. I sent the data and $event from the parent to the child as shown below. in the parent component: <v-filter :sortTypePrice="sortTypePrice" :sort ...

Using Node JS to retrieve JSON data from index.html upon button click

I'm currently learning the ropes of Node.js and want to set up a server where users can navigate to http://localhost:8000/ and be directed to index.html. In that file, there's a button to access JSON data. I plan to download this JSON data onto m ...

Displaying elements above my React sidebar

I am working on developing a Login application with a landing page using React Router and Redux. In order to have a Sidebar that is always present in all the components within the application, I have setup the Sidebar component as a route that is constantl ...

Discovering items within an array using Vue.js or JavaScript

I have an array of elements in Vue.js where I need to manipulate some data. For example, I have a select dropdown that displays company information with corresponding tags. These tags are at one sub level and are combined into one before being stored in th ...

Bring Component into Vue if necessary

I have multiple menu types and would like to determine which type of menu to use by configuring it in .env.local. For example: VUE_APP_MENU_TYPE=2 Here is the code snippet from my javascript file: let menu = false; if (process.env.VUE_APP_MENU_TYPE === &q ...

What steps can be taken to resolve the delay in transition when clicking on "read less

I implemented a transition effect for the Read More and Read Less buttons, however, there seems to be a delay on the transition when clicking on the Read Less button. This delay was not intentionally set by me. How can I resolve this issue and also apply a ...

Obtaining a complex object from a Checkbox set in AngularJS through the use of ngModel

Hey there! I've been searching on Stack Overflow regarding this topic, but I haven't found a solution that matches exactly what I need. If you want to take a look at the code, here is a JSFiddle link for reference: http://jsfiddle.net/gsLXf/1/ ...

What techniques can I use to generate a 3D visual effect by shifting the background image layer as the user scrolls (creating a Parallax effect

I have combined two images in the heading; one on top of the other. My goal is to create a subtle vertical movement in the background image as the user scrolls, giving the illusion of depth. Both images share the same width, but the background image is tal ...

Mastering AngularJS Authentication: A Step-by-Step Guide for the First Submission

I have a login function in Angularjs, but it only works when I submit it for the second time. How can I fix this issue? Here is my code: .controller('LoginCtrl', ['$scope', '$rootScope', '$location', 'Authenti ...