0
我有这个代码的问题进行并发API调用干:ReactJS和
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)和字符串作为参数来调用一个函数。不适合我。 你们能帮我解决吗?
你能与这两个参数分享您的尝试......这是最简单的解决方案,以缩短代码毫无疑问。 –