我想把getData(start)中的使用多个 if 这里优化一下,请问如何处理?

asyncgetData(start){
const_self=this;
varresponse=[];
if(start==0){
try{
//letresponse=awaitfetch('mock-data/selectData01.json').then(function(response){
//returnresponse.json();//第1个
//}).then(function(responseThenData){
////console.log('responseThenData=',responseThenData);
//returnresponseThenData//第2个
//}).catch(function(e){
//console.log("Oops,error");
//});
response=awaitfetch('mock-data/selectData01.json').then(function(response){
returnresponse.json();
})
returnresponse;//第3个
}catch(e){
console.log("Oops,error",e);
}
}elseif(start==1){
try{
response=awaitfetch('mock-data/selectData02.json').then(function(response){
returnresponse.json();
})
returnresponse;//第3个
}catch(e){
console.log("Oops,error",e);
}
}elseif(start==2){
try{
response=awaitfetch('mock-data/selectData03.json').then(function(response){
returnresponse.json();
})
returnresponse;//第3个
}catch(e){
console.log("Oops,error",e);
}
}elseif(start==3){
try{
response=awaitfetch('mock-data/selectData04.json').then(function(response){
returnresponse.json();
})
returnresponse;//第3个
}catch(e){
console.log("Oops,error",e);
}
}elseif(start==4){
try{
response=awaitfetch('mock-data/selectData05.json').then(function(response){
returnresponse.json();
})
returnresponse;//第3个
}catch(e){
console.log("Oops,error",e);
}
}
}
componentDidMount(){
const_self=this;
varkey=true;
letdataReadStart=0;
_self.getData(dataReadStart)
.then(function(responseThenData){
//console.log('responseThenData=',responseThenData['dataChinaTelecom']['regionData']);
_self.setState({
tableBody:responseThenData['dataChinaTelecom']['emergencyTableData'],
tableHead:responseThenData['dataChinaTelecom']['emergencyTableHeader']
})
})
.then(function(){
//console.log('abc')
})
.catch(function(e){
console.log("promise,error=",e);
});
//varkey=true;
//newPromise(function(resolve,reject){
//if(key){
//resolve('成功了')
//}else{
//reject('被拒绝')
//}
//}).then(function(value){
//console.log(value);//key=true123
//},function(rej){
//console.log('---------resultreject');
//console.log(rej);
//})
dataReadStart=dataReadStart+1;
setInterval(function(){
letdataPromise=_self.getData(dataReadStart);
console.log('dataPromise=',dataPromise)
dataPromise
.then(function(responseThenData){
//console.log('responseThenData=',responseThenData['dataChinaTelecom']['regionData']);
_self.setState({
tableBody:responseThenData['dataChinaTelecom']['emergencyTableData'],
tableHead:responseThenData['dataChinaTelecom']['emergencyTableHeader']
})
})
.then(function(){
//console.log('abc')
})
.catch(function(e){
console.log("promise,error=",e);
});
if(dataReadStart+1==5){
dataReadStart=0;
}else{
dataReadStart=dataReadStart+1;
}
},30000)
//ECMAScript定义了Undefined、Null、Boolean、String、Number、Object6种类型
//其中Undefined和Null都是只包含一个值得特殊类型,分别为undefined和null
//因此根据定义undefined和null分属不同类型
//使用===运算符返回false
//当声明的变量未初始化时,该变量的默认值是undefined,而null则用于表示尚未存在的对象
}
这里if能不能缩简到一个
HUX布斯
浏览 346回答 2
2回答

三国纷争

exportdefaultclassa{asyncgetData(start){varresponse=[];constMAP=[{url:'mock-data/selectData01.json'},{url:'mock-data/selectData02.json'},{url:'mock-data/selectData03.json'},{url:'mock-data/selectData04.json'},{url:'mock-data/selectData05.json'},];try{response=awaitfetch(MAP[start].url).then(res=>res.json());returnresponse;}catch(e){console.log('Oops,error',e);}}}抛砖引玉

凤凰求蛊

asyncgetData(start){try{constresponse=awaitfetch(`mock-data/selectData${(start+1+'').padStart(2,'0')}.json`)returnawaitresponse.json()}catch(e){console.log("Oops,error",e)}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript