Showing posts with label concurrent calls. Show all posts
Showing posts with label concurrent calls. Show all posts

Saturday, June 22, 2019

axios concurrent calls

Axios can make multiple concurrent calls. In app.js calls to two end-points are started at the same time so total waiting time is 4 seconds. Server latency is 4 seconds for demo purposes.

app.js

const axios = require('axios')
 
try {
    console.log("start");
    function getUserName() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    function getId() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    // Performing multiple concurrent requests waiting time 4 seconds
    axios.all([getUserName(), getId()])
        .then(axios.spread(function (acct, perms) {
            const user = acct.data[0].param;
            const id = perms.data[1].param;
            console.log(`${user} ${id}`)
            const str = 'https://jsonplaceholder.typicode.com/' + user + '/' + id
            console.log(str)
            axios.get(str).then(response => console.log(response.data.name));
        }));
} catch (error) {
    console.error(error);
}