gpt4 book ai didi

javascript - ReactJS 并使并发 API 调用 DRY

转载 作者:行者123 更新时间:2023-12-01 03:11:42 26 4
gpt4 key购买 nike

我的代码有问题:

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)并使用字符串作为参数来调用函数。对我不起作用。你们能帮我解决这个问题吗?

最佳答案

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

然后调用三遍,

getStuff('gists_url', 'contributorGists')
//... etc

关于javascript - ReactJS 并使并发 API 调用 DRY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45787373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com