I want to send multiple requests using Axios library. So, according to docs, I can do it with all
method. This is the example:
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now plete
}));
But why do I need to write
.then(axios.spread(function (acct, perms) {
// Both requests are now plete
}));
instead of
.then(function (acct, perms) {
// Both requests are now plete
});
if it also works fine?
I want to send multiple requests using Axios library. So, according to docs, I can do it with all
method. This is the example:
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now plete
}));
But why do I need to write
.then(axios.spread(function (acct, perms) {
// Both requests are now plete
}));
instead of
.then(function (acct, perms) {
// Both requests are now plete
});
if it also works fine?
Share Improve this question asked Nov 15, 2016 at 14:24 JustLoginJustLogin 1,8865 gold badges30 silver badges50 bronze badges1 Answer
Reset to default 6You need to use axios.spread
because it's used to spread the array of arguments into multiple arguments. This prevents errors when you are making multiple ajax requests with axios.all
.
axios.all([
axios.get('https://api.github./users/abc');
axios.get('https://api.github./users/abc/repos')
])
.then(axios.spread(function (userResponse, reposResponse) {
console.log('User', userResponse.data);
console.log('Repositories', reposResponse.data);
}));