javascript - ReactJS and making concurrent API calls DRY -
i'm having issue code:
let tmpcontributors = [...this.state.contributors]; (let = 0; < 10; i++) {//10 active contributors because of performance , 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 done 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; } } })) } (let = 0; < 10; i++) {//10 active contributors because of performance , 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 done 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; } } })) } (let = 0; < 10; i++) {//10 active contributors because of performance , 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 done 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; } } })) }
it works it's not dry. i've tried calling function 2 parameters (e.g. propertyurl, contributorproperty) , strings parameters. doesn't work me. can guys me one?
function getstuff(propertyurl, contributorproperty) { (let = 0; < 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; } } }) ) } }
then call 3 times,
getstuff('gists_url', 'contributorgists') //... etc
Comments
Post a Comment