Javascript ReactJS和使并发API调用干燥
我对此代码有问题:Javascript ReactJS和使并发API调用干燥,javascript,reactjs,frontend,Javascript,Reactjs,Frontend,我对此代码有问题: 让tmpContributors=[…this.state.contributors]; 对于(让i=0;i{ 如果(res.data.length>100){ tmpContributors[i].contributorFollowers=res.data.length; } 否则{ 对于(第页=1;第页){ 如果(res.data.length>100){ TMP Contributors[i]。contributorRepositories=res.data.len
让tmpContributors=[…this.state.contributors];
对于(让i=0;i<10;i++){//10个最活跃的贡献者,由于性能和github限制
contributorPropertiesPromises.push(axios.get(`${this.state.contributors[i].followers\u url}?per\u page=100&${API\u KEY}`)
。然后(res=>{
如果(res.data.length>100){
tmpContributors[i].contributorFollowers=res.data.length;
}
否则{
对于(第页=1;第页){
如果(res.data.length>100){
TMP Contributors[i]。contributorRepositories=res.data.length;
}
否则{
对于(第页=1;第页){
如果(res.data.length>100){
TMP贡献者[i]。贡献者=res.data.length;
}
否则{
对于(让page=1;page
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))
);
}
让tmpContributors=[…this.state.contributors];
常量getAxiosPromise=(索引、路径、查询=“”)=>
axios.get(
`${this.state.contributors[index][path]}?${query}per_page=100&${API_KEY}`
);
const dealWithResp=(res、path、index)=>{
如果(res.data.length>100){
tmpContributors[index].contributorFollowers=res.data.length;
}否则{
for(让page=1;页面处理resp(res,followers\u url,i))
);
贡献者属性promises.push(
getAxiosPromise(i,repos\u url)。然后(res=>dealWithResp(res,repos\u url,i))
);
贡献者属性promises.push(
getAxiosPromise(i,repos\u url)。然后(res=>dealWithResp(res,gists\u url,i))
);
}
这个怎么办:
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))
);
}
让tmpContributors=[…this.state.contributors];
常量getAxiosPromise=(索引、路径、查询=“”)=>
axios.get(
`${this.state.contributors[index][path]}?${query}per_page=100&${API_KEY}`
);
const dealWithResp=(res、path、index)=>{
如果(res.data.length>100){
tmpContributors[index].contributorFollowers=res.data.length;
}否则{
for(让page=1;页面处理resp(res,followers\u url,i))
);
贡献者属性promises.push(
getAxiosPromise(i,repos\u url)。然后(res=>dealWithResp(res,repos\u url,i))
);
贡献者属性promises.push(
getAxiosPromise(i,repos\u url)。然后(res=>dealWithResp(res,gists\u url,i))
);
}
你能用这两个参数分享你的尝试吗?这是毫无疑问地缩短代码的最简单方法。你能用这两个参数分享你的尝试吗?这是毫无疑问地缩短代码的最简单方法。
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))
);
}