首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactJS和使并发API调用变干

ReactJS和使并发API调用变干
EN

Stack Overflow用户
提问于 2017-08-21 06:33:15
回答 2查看 115关注 0票数 0

我对这段代码有一个问题:

代码语言:javascript
复制
let tmpContributors = [...this.state.contributors];
for (let i = 0; i < 10; i++) {//10 most active contributors because of performance and github limits
    contributorPropertiesPromises.push(axios.get(`${this.state.contributors[i].followers_url}?per_page=100&${API_KEY}`)
    .then(res => {
        if(res.data.length > 100) {
            tmpContributors[i].contributorFollowers = res.data.length;
        } 
        else {
            for(let page = 1; page <= 5; page++) {//5 pages because of github limitation - can be done by recursion checking if res.headers.link.includes('rel="next"')
                axios.get(`${this.state.contributors[i].followers_url}?page=${page}&per_page=100&${API_KEY}`)
                tmpContributors[i].contributorFollowers += res.data.length;
            }
        }
    }))
}
for (let i = 0; i < 10; i++) {//10 most active contributors because of performance and github limits
    contributorPropertiesPromises.push(axios.get(`${this.state.contributors[i].repos_url}?per_page=100&${API_KEY}`)
    .then(res => {
        if(res.data.length > 100) {
            tmpContributors[i].contributorRepositories = res.data.length;
        } 
        else {
            for(let page = 1; page <= 5; page++) {//5 pages because of github limitation - can be done by recursion checking if res.headers.link.includes('rel="next"')
                axios.get(`${this.state.contributors[i].repos_url}?page=${page}&per_page=100&${API_KEY}`)
                tmpContributors[i].contributorRepositories += res.data.length;
            }
        }
    }))
}
for (let i = 0; i < 10; i++) {//10 most active contributors because of performance and github limits
    contributorPropertiesPromises.push(axios.get(`${this.state.contributors[i].gists_url}?per_page=100&${API_KEY}`)
    .then(res => {
        if(res.data.length > 100) {
            tmpContributors[i].contributorGists = res.data.length;
        } 
        else {
            for(let page = 1; page <= 5; page++) {//5 pages because of github limitation - can be done by recursion checking if res.headers.link.includes('rel="next"')
                axios.get(`${this.state.contributors[i].gists_url}?page=${page}&per_page=100&${API_KEY}`)
                tmpContributors[i].contributorGists += res.data.length;
            }
        }
    }))
}

它很好用,但是不是很干。我尝试过使用两个参数(例如propertyUrl、contributorProperty)和字符串作为参数来调用函数。对我不起作用。你们能帮帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-08-21 06:46:29

代码语言:javascript
复制
function getStuff(propertyUrl, contributorProperty) {
    for (let i = 0; i < 10; i++) {
        contributorPropertiesPromises.push(axios.get(`${this.state.contributors[i][propertyUrl]}?per_page=100&${API_KEY}`)
            .then(res => {
                if(res.data.length > 100) {
                    tmpContributors[i][contributorProperty]= res.data.length;
                } 
                else {
                    for(let page = 1; page <= 5; page++) {
                        axios.get(`${this.state.contributors[i][propertyUrl]}?page=${page}&per_page=100&${API_KEY}`)
                        tmpContributors[i][contributorProperty] += res.data.length;
                    }
                }
            })
        )
    }
}

然后打三次电话,

代码语言:javascript
复制
getStuff('gists_url', 'contributorGists')
//... etc
票数 1
EN

Stack Overflow用户

发布于 2017-08-21 07:04:02

这样如何:

代码语言:javascript
复制
let tmpContributors = [...this.state.contributors];
const getAxiosPromise = (index, path, query = '') =>
  axios.get(
    `${this.state.contributors[index][path]}?${query}per_page=100&${API_KEY}`
  );

const dealWithResp = (res, path, index) => {
  if (res.data.length > 100) {
    tmpContributors[index].contributorFollowers = res.data.length;
  } else {
    for (let page = 1; page <= 5; page++) {
      getAxiosPromise(index, path, `page=${page}&`);
      tmpContributors[index].contributorFollowers += res.data.length;
    }
  }
};

for (let i = 0; i < 10; i++) {
  //10 most active contributors because of performance and github limits
  contributorPropertiesPromises.push(
    getAxiosPromise(i, followers_url).then(res => dealWithResp(res, followers_url, i))
  );
  contributorPropertiesPromises.push(
    getAxiosPromise(i, repos_url).then(res => dealWithResp(res, repos_url, i))
  );
  contributorPropertiesPromises.push(
    getAxiosPromise(i, repos_url).then(res => dealWithResp(res, gists_url, i))
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45787373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档