Enhancing $http Responses in AngularJS: Passing Extra Parameters

Is there a way to include additional configurations in $http service response? The code snippet below demonstrates the scenario:

var promises = [];
angular.forEach(rows, function(rowIterator) {
    var additionalConfig = {
      config1: rowIterator.config1,
      config2: rowIterator.config2
    }

    promise = $http({
        method: "get",
        url: "http://domain.com/" + rowIterator.videoId + '?appName=playlist',
        params: {}
    });
    promises.push(promise);                   
});

return $q.all(promises).then(function(data) {
    // How can I access fetched data along with the custom configs from 'additionalConfig'?
});

In this case, how do I extract and utilize the data retrieved by $q.all while maintaining the passed configurations from 'additionalConfig'?

Answer №1

Here's an interesting approach you could try to improve your code...

Consider chaining the response promises in a different way:
promises.push(promise.then(function(response) {
    return {
        response: response,
        additionalConfig: additionalConfig;
    };
}));

After that, you can utilize this sequence of actions:

return $q.all(promises).then(function(data) {
    angular.forEach(data, function(obj) {
        console.log('Response', obj.response);
        console.log('Additional config', obj.additionalConfig);
    });
});

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 eliminating excessive line breaks when incorporating a table tag into nl2br

In my dataset, I have content that consists of plain text, codes, and tables interchangeably. To keep the database optimized, I have limited the use of HTML tags by excluding pre tag for table and text, reserving it only for codes (spaces do not need to b ...

Looking to replace the cursor on your computer?

Upon exploring the angular-ui-tree, I observed that items are equipped with a drag or move cursor, which is helpful for making items draggable. However, I am curious about how to change the cursor to a normal arrow instead. Can anyone provide guidance on ...

Utilizing emailyak to send an email within an AngularJS function

After creating an emailyak account, I attempted to send an email using a JavaScript function. While I was able to successfully send an email with a curl command, I encountered difficulties when trying to replicate the same function in JavaScript. Here is ...

I'm looking for a PHP or Node.js OAuth library specifically for the Bitbucket API. I need to be able to make AJAX calls and interact with the library seamlessly

I am looking for a PHP-based library similar to loginWithBitbucket.php. This library should allow me to send authorization requests, which will trigger the opening of a new tab prompting the BitBucket user to enter their login credentials for authorizati ...

What is the correct method for storing or uploading an audio file to a designated destination?

Can anyone help me with storing recorded voices in a specific location and uploading the recorded files to that location? Below is a script for uploading an audio file using upload.php: Reference link: //upload link var upload = document.createElement( ...

What exactly is the purpose of utilizing node js and can someone explain to me what constitutes a

After mastering HTML and CSS, I've started diving into JavaScript. However, I'm curious about Node.js. What exactly is it, why do I need it, and can you explain what a framework is in general? Sorry if these questions sound silly! ...

Animating Chart.js 2 from right to left instead of from top to bottom

Here is the issue demonstrated in the jsfiddle below. Initially, the data inserts work well. However, when the data set reaches a cap of 10, an unwanted behavior occurs where the data points are animated top-down instead of moving leftward. This can be qu ...

On smaller screens, the top placement of right sidebar links is necessary

Our website layout is organized into 3 main sections: the top part contains the main menu, followed by the main content and a sidebar specific to each page. The main content and sidebar are structured using bootstrap col-* classes. On small screens, I en ...

Iframe not displaying Base64 encoded PDF in Chrome App

Currently, I am in the process of developing a Chrome App that essentially acts as a wrapper for the main app within a webview. The webview sends a Base64 encoded PDF as a message to the app, which then creates a hidden iframe and loads the PDF into the fr ...

How can I make the ion-list stay on top of the ion-footer-bar?

In the process of developing an application, I encountered an issue where the ion-footer-bar would cover the last record in the ion-list. After exploring various solutions, such as using `$scope.$broadcast('scroll.resize');`, the problem still pe ...

Tips for getting a jQuery click event to function properly within an AngularJS application

Although I have some experience with jQuery and can write basic jQuery code, I am currently learning AngularJS. I have been following tutorials and studying sample code. Now, I want to practice some basic tasks in Angular that I could easily do with jQuer ...

The attribute of the Angular div tag that lacks an equal sign

Apologies if this question has been asked before. I've noticed in some people's code that they use the following syntax: <div ui-grid="myUIGrid" ui-grid-selection ui-grid-resize-columns class="grid" /> Can someone explain what ui-grid-sel ...

YouTube's embedded video player is invincible and cannot be destroyed

Having trouble destroying an embedded YouTube video on the Plyr player. The player.destroy() method is being called without any errors, but it doesn't actually destroy the player. This issue is causing the previous video to load instead of a new one ...

Stop the context menu from popping up when the element is right-clicked

Is there a way to create a custom right-click interaction for an element on my website, <div class="myElement"></div>? I want to avoid the default context menu from popping up when the user right-clicks on this element in order to enhance the u ...

Numerous checkboxes have been chosen alongside a submission button

I recently completed a small project involving multiple checkboxes using ajax. You can view the demo here. However, I am now looking to implement a submit button for filtering options. After selecting multiple checkboxes and clicking the submit button, onl ...

Use a custom attribute in place of the val() method

My reliance on jQuery is minimal as I usually find what I need online. Currently, I am attempting to implement a live counting feature for a custom HTML attribute within a select/option element. While the script I have works well, I now require the abilit ...

Nodes in force-directed graphs are magnetically attracted to the central point

After finding a solution to the issue in this question Insert text inside Circle in D3 chart I have encountered an unexpected behavior where my nodes are not positioned correctly. I am unsure about which property is controlling the x and y coordinates of ...

disabling tabs that are next to each other using react hooks

When the user clicks on item one button in the first tab, it should disable the next two tabs. If the user clicks it back, it should enable the other two tabs. The same functionality should apply to the other tabs as well. Currently, I have disabled the ...

Searching does not reset when you click on a JavaScript onclick event

I have an interesting situation - I've set up a JavaScript onclick event that will parse JSON data from my MySQL database and display it when someone clicks on a seat. var jsonData = <?php echo json_encode($array); ?>; function displayInfoFo ...

Trouble with executing action after submitting form using jQuery and AJAX

I'm currently working on implementing form validation and submission using jQuery and AJAX. For the most part, everything is functioning as expected. However, there is a small snippet of code causing an issue: alert ('Validation success'); ...