Is there a way to pass promises to different middleware functions and get them resolved there?

In an attempt to streamline my code, I am working on a generic function that can manage promises efficiently.

Currently, I have promises set up in main.ts and when a request is received, I would like to pass these promises to the common function for execution using Promise.all.

However, I am encountering an issue where the array of promises passed to commonFunction is empty. Any guidance on resolving this challenge would be greatly appreciated.

main.ts

 public async execute(@Request() request: express.Request): Promise < [any] | any > {
     const promises: any = [];
     promises.push.apply(this.getAccountDetails(request), this.getCardDetails(request));
     return responseCollector(request, promises);
 }

 @Post('getAccountDetails')
 private async getAccountDetails(@Body() request: any): Promise < any > {
     const accountDetails: any = await axios.post(
        url, request.body);

   return accountDetails; 
 }

 @Post('getCardDetails')
 private async getCardDetails(@Body() request: any): Promise < any > {

     // process cardDetails Call and get response 
 }

commonFunction.ts

 export function responseCollector(expressReq, promises) {
     console.log("requestBody>>>", expressReq.body);
     console.log("promioses>>>>", JSON.stringify(promises));

     if (expressReq.body.lob === "credit") {
         return promises.Object
     }
     if (expressReq.body.lob === "account") {
         return promises.object
     }

     return Promise.all(promises)

 }

Answer №1

async function performAction(@Request() req: express.Request): Promise<[any] | any> {
     const tasks: any = [];
     tasks.push(fetchUserData(req), fetchCardData(req));
     return mergeResponses(req, tasks);
}

Avoid using .apply()

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

Utilizing React JS to Export Axios Response

I have an getAllUsers.js File that retrieves all users from the API. import axios from 'axios' export const fetchData = async () => { let response try { response = await axios.get('http://127.0.0.1:8000/api/users') } catc ...

The dataTable plugin is malfunctioning, displaying all records on a single page when it should only show 10 per page

I utilized the dataTable plugin to format my HTML table, but unfortunately it is not displaying the results as expected. Instead of showing 10 records per page, it is displaying all records at once. I even tried setting "iDisplayLength: 10" but it didn&apo ...

The function has been called but it did not return a

It seems that there is confusion surrounding the .toHaveBeenCalled() Matcher in Jasmine. While it should return a Promise that resolves when the function has been called, some users are experiencing it returning undefined instead. For example: it('sh ...

Ways to convert asynchronous operations of Node.js into synchronous operations in Node.js

Using node js, I am making multiple AWS API calls within a for loop. var prodAdvOptions = { host : "webservices.amazon.in", region : "IN", version : "2013-08-01", path : "/onca/xml" }; prodAdv = aws.createProdAdvCli ...

Limit the character count in a textarea

My goal is to control the number of characters that can be entered into a textarea. While I am aware that I could simply use a substring to limit the characters, I prefer to visually inform the user when their text has reached the maximum length. The maxl ...

Updating all images in a JQuery thumbnail gallery

I've been experimenting with jQuery and fancy box to create a special effect on my website. I wanted to display a large image with thumbnails below it, where clicking on a thumbnail would update the main image (similar to the RACE Twelve image example ...

In Safari, Angular2 domevents and rxjs callbacks occur outside NgZone

Upon completing a major upgrade on a client project that leverages Angular2+Polymer1, moving from Angular2 beta to stable, I encountered an issue where Angular's (eventname) DOM attribute and RxJS callbacks (subscribe, map) were triggering functions o ...

Having an issue with jQuery where trying to append a remove button to a list results in the

Looking to create a dynamic list where users can easily add new items by clicking a button. As each item is added, a corresponding remove button should also be displayed. The issue I'm facing is that every time a new item is added, an extra close but ...

Approach for fetching these JSON records

I am currently exploring ways to retrieve all entries within a JSON payload using JavaScript. Specifically, I am interested in finding the best method to extract all instances of "o4" in a specific order (-159, -257). How can this be achieved? { ...

React throws a "ReferenceError: indexedDB is not defined" but surprisingly, it still manages to function properly

I utilized yarn to install idb-keyval By utilizing the following code, I imported it: import { set } from 'idb-keyval'; Then, I assigned a value to a variable using the following code snippet: set('hello', 'world'); Althou ...

Alert: Error encountered while attempting to locate the Angular Material core theme within Angular 3, Angular Material, and Angular Material Experimental

After successfully implementing a custom color palette with Material 3 and Angular Material Experimental, I am encountering a warning in the console stating "Could not find Angular Material core theme. Most Material components may not work as expected. For ...

Issue with writing JSON data to a file in node.js

When I try to write JSON objects from the Twitter API to a file using the fs.appendFile method, all that gets written is "[object Object]". The JSON objects look fine when logged to the console, so I'm not sure why this is happening. For example, the ...

Save the uploaded file in Express to a custom directory on the system, avoiding the project's public folder

I have successfully integrated React on the front end to upload a file and receive it in Express.js. However, when storing the file in Express, it is currently being saved in the public folder. If I need to save the files in a non-public folder located at ...

Using TypeScript with Vue in a non-component-based architecture

Recently, I've been facing a challenge while developing an application. I'm using Vue + Vuetify with typescript, but I'm trying to steer clear of creating a single-page application or using webpack to handle .vue components. My goal is to cr ...

Exploring the ngModel directive implementation within a text input field

I'm facing an issue with displaying data in input forms. <div class="input-group mb-3"> <div class="input-group-prepend"> <span class="input-group-text text-white" style="background-color:#03a9f4">Product ID</span& ...

Expanding the Mui Typescript breakpoints within a TypeScript environment

Encountering a typescript error when attempting to utilize custom names for breakpoint values: Type '{ mobile: number; tablet: number; desktop: number;}' is not compatible with type '{ xs: number; sm: number; md: number; lg: number; xl: numb ...

Manipulate text with jQuery

Is there a way to remove 'http://' or 'https://' from text using javascript? I am looking for regex solutions as well. This is what I have tried so far: JSFIDDLE HTML: <div class="string"></div> JS: $text = $('.s ...

What is the best way to create a DOM element listener in AngularJS?

My goal is to monitor a <div id="please_monitor_me"> to determine if it is visible. If it is not visible, I plan to remove the margin (margin: 0;). I am aiming for no margin when the div is not visible so that it can be shown later with a burger but ...

unable to toggle the navbar

Currently, I am facing an issue with the navbar-toggle button while using Bootstrap. Initially, I thought the problem was with my navbar code, so I tried pasting the example from the Bootstrap Website, but it did not work either. The button appears, but t ...

Obtain the key's value from a JSON object that is nested within another

Managing a complex data structure is crucial in programming. How can I efficiently retrieve specific values using known keys from this nested data? Take, for instance: var data = { code: 42, items: [{ id: 1, category: [{ ...