Retrieving data from an array of resolved Promises

I'm using axios.all to iterate through an array of items and send a GET request for each one in order to store their data correctly. Currently, I have an array of Promises that are all resolving with the right data, and the callback function triggers once they're all done.

Now, my next step is to loop through these Promises and save their values, but I'm having trouble accessing those values!

let promises = [];

for (let report of response.data.res.body.result) {
    let dto2 = {
        customerId: state.member.customerId,
        reportToken: report.reportToken
    }

    promises.push(axios.post('http://localhost:3001/api/pullReport', dto2));
}
console.log("promises", promises);

axios.all(promises).then(function() {
    console.log("done!");
    promises.forEach(function(res) {
        console.log("res", res);
        // commit('SAVE_REPORT', res.value);
    })
    // resolve('Reports saved.');
});

When I console log each promise within the forEach loop, this is what it looks like:

__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object <<<<<<<<<<< NEED THIS

All I need is to trigger the commit('SAVE_REPORT') function with the PromiseValue Object, but I can't figure out what to pass in! I've tried res.value, res.val, res.promiseValue... Is there some trick to this?

Answer №1

When using axios.all, a new promise is created that will resolve with an array of the results. To access and work with these results, simply include a parameter in the function within the .then block:

axios.all(promises).then(function(results) {
    results.forEach(function(res) {
        commit('SAVE_REPORT', res.value);
    });
});

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 displaying video as background in angularjs

Trying to create a video background using Ionic and AngularJS. I attempted to insert this code into the HTML, but the video is not working: <source class="cvideo" src="background/{{current.currently.icon}}" type="video/webm"> When running it, the r ...

Improved method for transferring multiple form input values from a child to a parent window

I am working on a project where I have a child window containing multiple radio buttons. My goal is to send all the selected values from these radio buttons to the parent window. Currently, I am achieving this by creating a JavaScript string that contains ...

Error occurs in Javascript when attempting to execute javascript code using PHP's echo and an unexpected identifier token is encountered

Currently, I am trying to insert a div into the page when a specific condition is met in PHP: if ($var == 0) { echo '<script>console.log("Test."); var res = document.getElementById("response"); res.innerHTML = "<div class='h ...

Limit how API call costs are set in a function by throttling based on an argument

I am currently implementing express-throttle to restrict the number of API calls per IP address each day. I would like to dynamically set the 'cost' parameter in the 'options' array based on a value from the API request (refer to commen ...

Executing CORS request using Node.js/Express and AngularJS

I've come across multiple responses on Stack Overflow claiming that setting response headers will resolve CORS requests. However, none of the solutions have worked for me. Here is the code I have implemented: //Server.js Code var express = require(&a ...

Kohana ajax causing removal of JQuery Data attributes

Currently, I am developing an application where jquery data is used to pass variables to HTML elements. It has been successful in one part of the site when the data attributes are added to a tr tag. The following code works: <tr class="js-instructions ...

Using object in TypeScript to reduce arrays

Is there a way to set the return value for my reducer in TypeScript? I am looking to achieve: Instead of using 'any', what should I assign as the type for acc? How can I define my return type so that the output will be {temp: 60, temp: 60}? retu ...

When attempting to host a web application built using the Impact JavaScript game engine on a local server, Chrome throws CORS errors

Currently, I am working on a webapp created with the Impact JS game engine that needs to run locally without using a localhost (using file:///C:/...). The issue I am facing is with Chrome, as it is blocking the loading of images (mainly png/jpg) from the m ...

Utilizing analytics.js independently of the react-ga library

Is there a way to integrate Google Analytics into my React app without using the react-ga package? I specifically want to access the ga function in a separate JavaScript file as well as within a React component. How can I achieve this by importing the anal ...

What is the best way to identify the appropriate element to display based on the anchor inside the element?

Incorporating various Twitter Bootstrap collapse groups on my website is a key feature. These groups consist of multiple anchors: <div class="accordion" id="accordion"> <div class="accordion-group"> <div class="accordion-heading" ...

Unable to alter Mui input label color upon focus using theme.ts

In my next.js app, I'm facing an issue with changing the color of the label in a Material UI input field using Mui. Despite updating the theme.ts file to change the border bottom color and label color, only the border bottom color is being applied. T ...

Is it possible to determine if a JavaScript/jQuery effect or plugin will function on iPhone and iPad without actually owning those devices?

Is there a way to ensure that a javascript/jquery effect or plugin that works well on all desktop browsers will also work on iPhone and iPad without actually owning those devices? Can I rely on testing it on the latest Safari for Windows to guarantee comp ...

Unable to set the active tab using $rootScope

Is there anyone who can assist me with the issue I am currently facing? I have a NavCtrl to manage my active tags, and although I was able to change the active tab when clicking on the menu item, it does not update when clicking on links in the body views. ...

Extracting data from a JSON string within a TXT document

I am looking to allow the user to select a file that will be read and parsed into JSON for storage in their localStorage. However, when reading from the file, each character is interpreted as a key, unlike when directly pasting the JSON string into the fun ...

Showing a message only when there is input in the search bar using jQuery

I'm currently working with a Google Instant style search script that is written in jQuery and queries a PHP file. The issue I am facing is that even when the search box is empty and there are no search terms, the script still displays a message saying ...

The beauty of asynchronous GET requests in VueJS

As a newcomer to VueJS, I am exploring how to make a GET request to the GitHub API. Initially, I made a request to sort users by follower count, resulting in an array ordered in descending order of user logins. Following that, I sent another GET request to ...

Update a MongoDB collection with data retrieved from a JavaScript object mapping

I have come across a collection that has the following structure: [ { "project":"example1", "stores":[ { "id":"10" "name":"aa", ...

How to implement a select all feature using React and MaterialUI

I am facing a challenge with managing multiple sets of checkboxes independently along with a toggle that can switch them all on or off within their respective groups. Each checkbox has two states, and I can identify the checkbox being clicked using event.t ...

Guidance on utilizing jQuery to display values depending on a dropdown selection

This code snippet displays movie data from a JSON variable in a dropdown list based on the selected city. It also needs to show the movie name and theaters list when a user selects a movie, along with the theater dropdown remaining unchanged. Here is my H ...

Navigating through an array of objects with Node.js

Recently, I integrated the ChartJS library into my Node web app to visualize data. The following is nested in a script tag on an EJS template page: <script> let playerStatChart = new Chart(myChart, { type: 'bar', data: { la ...