最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why do I need to use spread function in axios.all callback? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

You 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);
}));
发布评论

评论列表(0)

  1. 暂无评论