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

Could someone provide a detailed explanation of exhaustMap in the context of Angular using rxjs?

import { HttpHandler, HttpInterceptor, HttpParams, HttpRequest, } from '@angular/common/http'; import { Injectable } from '@core/services/auth.service'; import { exhaustMap, take } from 'rxjs/operators'; import { Authe ...

In AngularJS, what is the best way to add a filter inside another filter?

Struggling to properly unit test a filter that relies on another filter. Despite attempts, I have been unsuccessful in mocking the filter. While I've come across methods for mocking filters in controllers and testing them in isolation, this particular ...

Printing the result of an SQL query in Node.js without using braces

As a beginner in node.js with only a basic understanding of javascript, I apologize if I make any mistakes. I attempted to display the results retrieved by an SQL query in node.js using: <p>Users are " + util.inspect(results) + ".</p>" an ...

Retrieve the HTML contents of a cell that contains a checkbox with the value of "jquery"

Here is an example of a table row: <tr> <td><input type='checkbox' name='post[]' value="1"></td> <td>08-Apr-2014</td> <td>injj team</td> <td>merchant.testyy.com</ ...

How can Vue 3's v-bind=$attrs be implemented effectively?

I am currently in the process of migrating my Vue 2 application to Vue 3. According to the official documentation, the $listeners object has been removed in Vue 3 and event listeners are now part of $attrs. This includes taking non-prop attributes like cla ...

Combining strings within a string after a specific word with nested Concatenation

In a given string variable "str," I am looking to concatenate another string between "InsertAfterMe" and "InsertBeforeMe". str="This is a string InsertAfterMe InsertBeforeMe" s1="sometext" s2="soMoreText" aList=[1,2,3,4,5] The concatenated string shoul ...

What is the reason for the failure of this JavaScript promise?

Currently studying the concept of promises. app.get('/message',function(req, res){ var promise = new Promise(function(resolve, reject){ resolve("hi"); }); promise.then(function(message){ res.json(message); }) ...

Error encountered while using the Fetch API: SyntaxError - the JSON data contains an unexpected character at the beginning

I've been troubleshooting a contact form and there seems to be an error causing it to malfunction. It's strange because when I switch from Fetch to XMLHttpRequest, the code works fine. With Fetch, if I don't request a response, no errors ar ...

Ingesting RSS feed into an Express server

I've been searching for hours, but I just can't seem to find a solution. I was able to figure things out when working on the client side, but now that I'm trying to load posts on the server and render them in the view, I'm hitting a roa ...

How to prevent uncaught errors when checking for undefined in if statements and dealing with undefined items

It appears that there are not many oboe tags being used on SO, but any assistance with this general JavaScript question regarding handling uncaught errors for undefined would be greatly appreciated!~ I am currently utilizing Oboe.js to stream data to a we ...

Using Javascript to dynamically add variables to a form submission process

Looking to enhance my javascript skills, I've created a script that locates an existing id and exchanges it with a form. Inside this form, I'm aiming to incorporate javascript variables into the submit url. Unsure if this is feasible or if I&apo ...

Creating a webpage that dynamically loads both content and video using HTML and Javascript

I designed a loading page for my website, but I could use some assistance. The loading page remains visible until the entire HTML content is loaded and then it fades out. However, I am facing an issue because I have a background video that I want to load ...

Rephrase the ajax call and the data retrieved

I'm struggling to find a solution for writing this code snippet without using async: false,. var imageX; var groupX; $.ajax({ type:'GET', url:'php/myphp.php', dataType:'json', async: false, success: ...

Sending a PHP string formatted as JSON to be processed by jQuery

When attempting to echo a string with a JSON structure, I am encountering issues with the jQuery ajax post not parsing it correctly. My question is whether this string will be recognized as JSON by the jQuery json parse function when echoed in a similar ma ...

Maintain the selected image

I am working on a program that allows users to choose images and I have given them the pseudo-class. .my_image_class:hover{ border:3px solid blue; -webkit-box-sizing: border-box; } This makes the images bordered in blue when hovered over, giving a "sele ...

When using Vue.js, binding may not function properly if it is updated using jQuery

Link to JsFiddle Below is the HTML code: <div id="testVue"> <input id="test" v-model="testModel"/> <button @click="clickMe()">Click me</button> <button @click="showValue()">Show value</button> </div& ...

Having trouble getting tailwind dark mode to work on next.js?

I have set up a custom boilerplate using next.js(10.0.5) with preact(10.5.12), typescript(4.1.3), and tailwind(2.0.2). I am attempting to incorporate a dark mode feature from Tailwind. I followed the instructions from next-themes in order to add the dark ...

What are the steps for utilizing ckeditor to send textarea information via ajax?

Here is the code snippet that controls the textarea in my chat application: <div class="chat"> <div class="messages"></div> <textarea class="entry" name="entry" placeholder="Welcome to the Chat. Enter your message here!">&l ...

The slider thumb is not showing up properly on Microsoft Edge

Hey there! I'm experiencing an issue with the range slider's thumb display in Microsoft Edge. It appears to be positioned inside the track rather than outside as intended. Take a look at this snapshot for clarification: https://i.stack.imgur.co ...

The design of RESTful web applications with a client-server architecture

I need clarification on how client-server architecture should function for a modern web application with a RESTful backend. For a web app, the client is typically the browser while the server is the web server. Programatically speaking, there are componen ...