ReactJS并使并发API调用DRY

时间:2017-08-20 22:33:15

标签: javascript reactjs frontend

我对此代码有疑问:



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;
            }
        }
    }))
}
&#13;
&#13;
&#13;

它有效,但它不是很干。我尝试使用两个参数调用函数(例如propertyUrl,contributorProperty)并使用字符串作为参数。对我不起作用。 你们可以帮帮我吗?

2 个答案:

答案 0 :(得分:2)

log <- read.csv(file = "my.log", sep = "\n", header = F, fileEncoding = "UTF-16LE", encoding = "UTF-8")

然后调用它三次,

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;
                    }
                }
            })
        )
    }
}

答案 1 :(得分:0)

这个怎么样:

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))
  );
}