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.

axios.all() after response send a new request

See original GitHub issue

How can i send a new request using axios until axios.all() response has not come. I have three type of request need to load when app is load first time. After response of each api i want to send a new request using these api response data.

Here is my code that return network error 500

export function appInitialize(navigator) {
    return async function (dispatch) {
        dispatch({ type: APP_INITIALIZE });
        axios.all([
            api.get('appload'),
            api.get('admin-settings'),
            axios.get('https://example.com/api/ipinfo')
        ]).then(axios.spread((appLoadRes, adminSettingsRes, ipRes) => {
            dispatch({
                type: APP_INITIALIZED,
                payload: {
                    appload: appLoadRes.data.response.data,
                    adminSettings: adminSettingsRes.data.response.data,
                    ip: ipRes.data.ip
                }
            });
            dispatch(changeAppRoot('after-login'));
            getUser().then(user => {
                if (user) {
                    dispatch(changeAppRoot('after-login'));
                    api.get(`user/${user.id}`, {
                        headers: {
                            'Authorization': `Bearer ${user.token}`,
                            'Ip': ipRes.data.ip
                        }
                    }).then(response => {
                        dispatch({ type: USER_MANAGED, payload: response.data })
                    })
                    navigator.resetTo({
                        screen: 'navigation.Dashboard'
                    })
                } else {
                    navigator.resetTo({
                        screen: 'navigation.Login'
                    })
                }
            })
        }))
    };
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
anthonygauthiercommented, Aug 16, 2018

Hey there @shubham81407,

You could try something like this;

export function appInitialize(navigator) {
  return async function (dispatch) {
    dispatch({ type: APP_INITIALIZE });
    let promises = [];

    promises.push(axios("url1"));
    promises.push(axios("url2"));
    promises.push(axios("url3"));

    const responses = await Promise.all(promises);
    const responseDataArr = responses.map(response => response.data);

    // make your request with the content of the responses here
    // axios.get('...')
  }
}

What do you think?

1reaction
OpenGGcommented, Aug 22, 2018

For these kind of general question, please consider asking on StackOverflow. But feel free to come back and open an issue if it turns out to be a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using axios.all to make concurrent requests - LogRocket Blog
The axios.all function accepts an iterable object that must be a promise, such as a JavaScript array, and it returns an array of...
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 >
How to Perform HTTP Requests with Axios – A Complete Guide
Axios allows us to send multiple requests at the same time. To do so, we'll use the Promise.all() function in this tutorial to...
Read more >
Making HTTP requests with Axios - CircleCI
Axios works by making HTTP requests with NodeJS and XMLHttpRequests on the browser. If the request was successful, you will receive a response...
Read more >
Make Multiple Post Requests In Axios then get all the ...
I made a very simplified example of what you're trying to accomplish using async/await syntax since .then() would be messier to read; ...
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