What is the best way to set up a promise for an HTTP GET request?

How can promises be implemented around the provided http get request?

$http({
    method: 'GET',
    url: 'getData.php',
    params: {bill: 'Active'}
})
.then(function (response) {
    bill=response.data.results;
});

There is a need for the processing to wait until all the http requests are completed. To achieve this, I want to implement promises and then return promises at the end of the function. What would be the best way to go about implementing promises in this scenario?

Answer №1

const requests=[];
requests.push(fetchData());
requests.push(fetchData());
requests.push(fetchData());

Promise.all(requests).then(....)

function fetchData(){
return fetch({
    method: 'GET',
    url: 'retrieveData.php',
    params: {status: 'available'}
})
}

Answer №2

The code you've written (everything before the .then(...)) is already functioning as a promise.

To consolidate your array of requests, consider using Promise.all to create a new promise that resolves only when all tasks are completed.

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

Incorporating a new dropdown menu above the content of a pre-existing jQuery accordion dropdown, causing the content to shift downwards

Seeking a way to overlay a second-level dropdown menu on top of the content beneath a jQuery accordion-style dropdown. Typically, the jQuery accordion pushes down the content below, but I want the second-level dropdown to remain unaffected. Is there a solu ...

Ways to access a JavaScript function on a div with multiple identifiers

Currently working on developing a discussion panel using ASP.net, where each comment is assigned a unique id. I am looking to trigger the jQuery click function whenever a user clicks on a comment. However, since the comment ids are dynamically assigned an ...

I am attempting to implement a feature that changes the color of buttons when they are clicked within an ng-repeat loop

A problem is occurring with the ng-class directive in this HTML code, which is generating seats using the ng-repeat directive. The colors are not being added properly when a seat is selected. If you'd like to view my code, it can be found in this JSf ...

JavaScript call is not appearing during execution on asp.net

In the aspx page, there is an input element that looks like this: <input id="txtAllocAmt" type="text" class="txtAllocAmt" tabindex="2" size="10" name="Text1" runat="server" disabled="disabled" onfocus="javascript:SetOldAllocAmt(t ...

Using Vue to download a file by linking to the href and incorporating a Vuetify v-btn

Having recently started learning Vue.js, I am currently encountering difficulty with a seemingly simple task. My goal is to add a Vuetify button to my webpage that allows users to download a file. However, I am struggling with getting the href attribute t ...

Transmit data via AJAX to the RequestBody

Although seemingly simple, this issue has proven to be quite challenging. I am confident that it can be easily resolved, but the solution continues to elude me. Thank you for any assistance you can provide. Here is the code snippet in question : var samp ...

Ways to prevent the submitted post from disappearing upon reloading in HTML

Currently, I am in the process of developing a website. One of my goals is to ensure that a specific post remains visible even after the page is reloaded. I want the post to persist without disappearing upon reloading. Can anyone provide guidance on how ...

Locate the div in the array that includes ValueA, and save ValueB from it into a new variable

When the button is clicked, I retrieve the current value of a Select control using the following code... $('#myBtn').on('click', function (clickEvent) { var nameSelected = document.getElementById('mySelectControl').getEle ...

Incorrect object being returned from Angular 2 HTTP request

For my data fetching from the server, I am using @angular/http get method. Below is the code snippet: private _currentPT: any; public phongtroDetailChange = new Subject(); layPhongtro(id: number): Promise<any> { return new Promise((resolve, reject) ...

JavaScript switch statement and case expression

Struggling to use boolean expressions within a switch statement to search for undefined values and manually change them. When using an if statement, the process is easier. For example: if statement if(Item1 == undefined) { item1 = "No"; } else if (Item ...

Automated Recommendation Selector

I'm searching for a JavaScript library that can provide the following functionalities: An auto-suggest dropdown list that uses Ajax to retrieve results from a database. Restricting the user to only select values provided by the Ajax call. Abilit ...

There seems to be an issue with rendering or redirecting in Jade files, the routes folder, and server/app.js

//routes/meal.js var express = require('express'); var router = express.Router(); var app = express(); /* GET users listing. */ router.get('/meal', function(req, res, next) { res.render('/home/noah/Desktop/gadfit/views/meal.jad ...

Hide the div once it goes off screen, ensuring that the user stays in the same position on the page

On my website, I implemented an effect similar to the Airbnb homepage where there is a "How it Works" button that toggles a Div element pushing down the entire page. However, when the user scrolls to the bottom of the toggled div (#slideDown) and it disapp ...

What is the best way to transform a GET request with a query string into a promise?

After successfully making a request with querystring params, I encountered an issue while trying to promisify my request: // Works var Promise = require("bluebird"); var request = Promise.promisifyAll(require("request")); request.get({ url: 'htt ...

Issue with nivo-lightbox not opening upon clicking image

I have diligently followed the instructions to set up this JavaScript plugin, but unfortunately it doesn't seem to be functioning properly. The plugin I'm using can be found here: All the links to the CSS, theme, and JavaScript files are display ...

Struggling to map a JSON file in a NextJS project using Typescript

I'm a beginner in JS and I'm currently struggling to figure out how to display the data from a json file as HTML elements. Whenever I run my code on the development server, I encounter the error message: "props.books.map is not a function&q ...

The dropdown feature in Safari fails to collapse when an option is selected

I've encountered an issue with my code using Bootstrap 4 and it's functioning correctly on IE 11 and Firefox, but not on Safari for Mac. The content remains hidden when selecting an option from the dropdown menu in Safari. My intention is to hid ...

Use `Res.download()` to send data to the client instead of directly downloading the file

I am trying to transfer a file that has been created by my NodeJS server from the server to the client for download. Users input object data which is then stored in a database. The function responsible for creating the file will read these objects and pop ...

On the hunt for a specialized JavaScript library for input validation?

Have you ever come across a JavaScript library that allows for variable validation in a convenient chaining style like this one? For example: if(insertLibraryNameHere(myNumberInput).int().min(0).max(10)) This library also has checks for other data types ...

"Using Vue's v-model directive in conjunction with a select input

I'm attempting to set any option within my select input as a value in an object. My goal is to utilize v-model for this, but I'm still unsure of the process. Below is my current attempt: <div class="select-wrapper"> <select r ...