Nested Promise.all within another Promise.all appears to terminate prematurely, triggering a warning indicating failure to return from a promise

I am utilizing this function to be invoked within another Promise.all. However, I consistently encounter a warning message: Caution: a promise was generated in a handler but was not returned from it. Additionally, the function deleteFutureAppointments() appears to depart from the original promise.all and commences executing other tasks within that promise.all sequence.

function deleteFutureAppointments() {
Appointment.findAll(
{ where: 
    { 
        pro_id, client_id, from_datetime_utc: { 
            $gt: new Date() 
        }
    } 
})
.then((appointments) => {
    if (!appointments) {
        return new Promise((resolve) => resolve());
    }          
    const promises = appointments.map((id) => {
        const appointmentQuery = { where: { client_id } };
        const appointmentSMIQuery = { where: { appointment_id: id.get("appointment_id") } };
        return AppointmentMedia.destroy(appointmentSMIQuery)
        .then((result) => {
            if (result) {
                removeAppointmentMedia.push(id.get("appointment_id"));
            }
            AppointmentService.destroy(appointmentSMIQuery);
        })
        .then(() => IndexAppointmentCalendar.destroy(appointmentSMIQuery))
        .then(() => Appointment.destroy(appointmentQuery));
    });
    return Promise.all(promises);
})
.catch((err) => {
    next(err);
});
}

Answer №1

It seems that the issue might lie in not returning the Promise from AppointmentService.destroy when calling deleteFutureAppointments(). To address this, I suggest restructuring your code as follows:

function deleteUpcomingAppointments() {
  Appointment.findMany({ where: {
    pro_id, client_id,
    from_datetime_utc: { $gt: new Date() }
  } }).then(appointments => {
    return Promise.all(appointments.map(appointment => {
      var id = appointment.get("appointment_id");
      const appointmentSMIQuery = { where: {
        appointment_id: id
      } };
      return AppointmentMedia.delete(appointmentSMIQuery).then(result => {
        if (result)
          removeAppointmentMedia.push(id);
        return AppointmentService.destroy(appointmentSMIQuery);
      })
      .then(() => IndexAppointmentCalendar.destroy(appointmentSMIQuery))
      .then(() => Appointment.remove({ where: { client_id } }));
    }));
  })
  .catch((err) => {
    handleError(err);
  });
}

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 AngularJS: Component controller fails to load upon routing using ngRoute

As a newcomer to AngularJS, I am struggling with the following code snippet: This is the component defined in the JavaScript file provided: angular.module('EasyDocsUBBApp') .component('loginTag', { templateUrl: 'login ...

A technique for calculating the total quantity of each item individually while using v-for in VueJS

Struggling to code (complete newbie) using VueJS and facing a major roadblock. I have a list of orders and I need to sum the quantities of each item separately. The only way to access the items is through v-for. <tr> <td data-th="list"> < ...

Guide to successfully setting up a Json server and ReactJS to run simultaneously on the same port

Currently, I am facing an issue while attempting to run my React application with a JSON server as the backend API. The problem arises when trying to run them on different ports due to a CORS error. I am currently seeking advice and looking for a solutio ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

Instructions on obtaining a distinct daily array from the weather API array that provides a detailed 5-day weather report every 3 hours

Seeking assistance with my weather app development on React using axios with the openweathermap.org API. Currently stuck on getting data formatted in a specific way from the 5-day forecast it provides, totaling 40 reports over the 5 days. The API response ...

Sinon respects my intern functions during testing in ExpressJS

At the moment, I am working on incorporating sinon stubs into my express routes. However, I am facing an issue where my functions are not being replaced as expected. I would like my test to send a request to my login route and have it call a fake function ...

Express server experiencing a backlog of POST requests after receiving six requests from the same browser, resulting in unexpected or unusual behavior

My application is experiencing delays in processing post requests when multiple requests are sent within a short timeframe. This results in unexpected behavior. For instance: app.options('/',cors({origin: "http://localhost:5050"})); ap ...

Utilizing an SSL certification (pem file) within JavaScript

Currently in the process of developing a program to extract data from the FAA's AIDAP database. I received a security certificate in the form of a .p12 file, which I have successfully converted into a .pem file. However, I am encountering difficulties ...

What guidelines should be followed for utilizing jQuery's Ajax convenience methods and effectively managing errors?

Imagine a scenario where I am trying to mimic Gmail's interface using jQuery Ajax to incorporate periodic auto-saving and sending functionalities. Error handling is crucial, especially in cases of network errors or other issues. Instead of just being ...

What is the most efficient way to retrieve the key at a specific index within a JavaScript map object?

If I have the map object shown below: const items = new Map([['item1','A'], ['item2','B'], ['item3', 'C']]) I am trying to retrieve the key at index 2. Is there a method other than using a for ...

Troubleshooting: Issues with Material-UI's Disabled attribute functionality

I am facing an issue with disabling the edit button after clicking on complete. I have tried passing the state in the disabled attribute, but it doesn't seem to work. I suspect it may be due to the asynchronous nature of setState. Additionally, when p ...

Integrate a button following the first paragraph exclusively when there are two or more paragraphs present

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> jQuery(document).ready(function($) { if ( $('p').length < 1 ) { $('p:last-child').after('<div id="toggle" class="btn"> ...

Encountering issue: LineChart is not recognized as a constructor, resulting in failure to display chart on webpage

I am struggling with displaying a chart in my HTML file that should show the details of a specific process from the past few minutes. Whenever I try to initialize the chart using google.charts.load('current', {'packages':['line&apo ...

standalone visuals generated interactively with matplotlib

While I appreciate the plots generated by matplotlib and the ability to save them as SVG, there is a feature missing that I would like to see added... I am looking for a way to save the figure as an SVG file with embedded JavaScript code to add interactiv ...

building responsive servers within Gulp using connect

Can I validate the availability of a server port before creating it using Gulp? Currently, this is my approach: /** * Start LiveReload Server */ gulp.task('connect', function() { var connect = require('connect'), app = ...

Combining Vue.js for handling both enter key and blur events simultaneously

I have been working on a solution where pressing the enter key or losing focus on an element will hide it and display a message. However, I am facing an issue where when I press the enter key to hide the element, it also triggers the blur event. I only wan ...

Unable to utilize ES6 syntax for injecting a service

I am encountering some issues while trying to implement a service into a controller using ES6 syntax. CategoriesService.js export default class CategoriesService { constructor() { this.getCategories = function ($q) { var deferred ...

What is the mechanism by which a custom hook, functioning as a trigger, initiates the re-rendering of a separate function component?

According to the official documentation on Custom React Hooks, one particular use case for utilizing a custom hook is demonstrated through the following example: function FriendListItem(props) { const isOnline = useFriendStatus(props.friend.id); retur ...

Organize library files into a build directory using yarn workspaces and typescript

When setting up my project, I decided to create a yarn workspace along with typescript. Within this workspace, I have three folders each with their own package.json /api /client /lib The main goal is to facilitate code sharing between the lib folder and b ...

Is there a way to prevent ng-template-loader from scanning image src's?

Currently, I am beginning to incorporate webpack into my development workflow for an angular project. To create my templateCache, I have had to include the ng-template-loader. Below is a snippet of my webpack configuration: { test: /\.html$/, loa ...