question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chaining axios call based on array

See original GitHub issue

How do I do this dynamically

var array = [0,1,2];
axios.get('api/' + array[0]).then(response => {
    axios.get('api/' + array[1]).then(response => {
         axios.get('api/' + array[2]).then(response => {
            alert('done');
         });
    });
});

how do I chain axios call based on array?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

23reactions
Axnyffcommented, Mar 18, 2018

Something like this should work:

const axios = require('axios');
function makeRequestsFromArray(arr) {
    let index = 0;
    function request() {
        return axios.get('http://localhost:3000/api/' + index).then(() => {
            index++;
            if (index >= arr.length) {
                return 'done'
            }
            return request();
        });

    }
    return request();
}

makeRequestsFromArray([0, 1, 2]);
0reactions
coder618commented, May 14, 2020

uld you mind elaborating ?

Nothing to worry, I am just trying to say if anyone wants to add specific code as per his/her project need then those codes will go above . index++;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Axios: chaining multiple API requests - reactjs - Stack Overflow
axios.spread() is used to spread the array of arguments into multiple arguments, so that all data can be passed to the function.
Read more >
How to send multiple requests using axios - Storyblok
Let us start with a small task and sending one request using Axios itself. First, we import axios and define the API/URL we...
Read more >
Using axios.all to make concurrent requests - LogRocket Blog
This request returns an array as a response and the data in the array is ordered according to our endpoints array, meaning Axios...
Read more >
Use Promise.all to Stop Async/Await from Blocking Execution ...
An async chain: one operation that loads an array, then a series of calls that depend on the result of the original call....
Read more >
Axios React – How to Make Get, Post, and Delete API Requests
Axios [https://axios-http.com/] is an HTTP client library based on promises. It makes sending asynchronous HTTP requests to REST endpoints ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found