我需要制作一个导入记录的进度条,该记录是用户从前端发送到服务器的(在春季启动时)。
第一个请求将导入数据库(后请求)。我用来了解插入进度的第二个请求。因此,有时我需要发送有关导入进度的第二个请求,但是我失败了。
问题是,在启动第一个请求之后,什么也没发生(我在服务器上的输出中看到了这一点,即已导入行,就是这样),整个接口都冻结了
当第一个请求在浏览器中发送并且第二个请求的答案返回给我时,我试图在失眠中发送第二个请求。当我在浏览器中同时发出两个请求时,没有响应。
代码(反应):
async sendRecordsToServer() {
let stateSaved = this;
const response = await axios({
method: 'post',
url: /*url*/,
data: JSON.stringify(this.state.selectedData),
headers: /*headers*/
});
const json = await response.data;
if (json) {
console.log("response from server 1 request if");
console.log(json);
stateSaved.setState({
countOfImportedRows: json.length
});
stateSaved.setState({
modalStateShowInfoImport: "show"
});
return true;
} else {
console.log("response from server 1 request else");
console.log(json);
stateSaved.setState({
modalStateShowActiveSession: "show"
});
return false;
}
}
async getLastIdInTable() {
let stateSaved = this;
const response = await axios({
method: 'get',
url: /*url*/,
headers: /*headers*/
});
const json = await response.data;
console.log("response from server 2 request");
console.log(json);
return json;
}
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async _sendData(){
const lastIdInTableBeforeImport = await this.getLastIdInTable();
let countOfAlreadyImportedRows = 0;
const responseFromImporting = await this.sendRecordsToServer();
while (countOfAlreadyImportedRows != this.state.data.length) {
this.sleep(3000).then(async () => {
const lastIdInTableCurrent = await this.getLastIdInTable();
countOfAlreadyImportedRows = lastIdInTableCurrent - lastIdInTableBeforeImport;
console.log("in loop");
console.log(countOfAlreadyImportedRows);
});
}
}
UPD:
对代码进行一些更改并添加新内容console.log()
以显示更多详细信息。
码:
async _sendData(){
const lastIdInTableBeforeImport = await this.getLastIdInTable();
let countOfAlreadyImportedRows = 0;
const responseFromImporting = this.sendRecordsToServer();
console.log("after sendRecordsToServer()");
while (countOfAlreadyImportedRows != this.state.data.length) {
console.log("before sleep");
this.sleep(3000).then(async () => {
console.log("in sleep");
const lastIdInTableCurrent = await this.getLastIdInTable();
countOfAlreadyImportedRows = lastIdInTableCurrent - lastIdInTableBeforeImport;
console.log("in loop");
console.log(countOfAlreadyImportedRows);
});
}
}
async sendRecordsToServer() {
let stateSaved = this;
const response = await axios({
method: 'post',
url: /*url*/,
data: JSON.stringify(this.state.selectedData),
headers: /*headers*/
});
// const json = await response.data;
// if (json) {
// console.log("response from server 1 request if");
// console.log(json);
// stateSaved.setState({
// countOfImportedRows: json.length
// });
// stateSaved.setState({
// modalStateShowInfoImport: "show"
// });
// return true;
// } else {
// console.log("response from server 1 request else");
// console.log(json);
// stateSaved.setState({
// modalStateShowActiveSession: "show"
// });
// return false;
// }
}
async getLastIdInTable() {
let stateSaved = this;
const response = await axios({
method: 'get',
url: /*url*/,
headers: /*headers*/
});
const json = await response.data;
console.log("response from server 2 request");
console.log(json);
return json;
}
sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
尝试这个:
async _sendData(){
const lastIdInTableBeforeImport = await this.getLastIdInTable();
let countOfAlreadyImportedRows = 0;
const responseFromImporting = await this.sendRecordsToServer(); // let's add `await` back, it isn't the murder.
console.log("after sendRecordsToServer()");
const timerId = setInterval(()=> {
if(countOfAlreadyImportedRows !== this.state.data.length) {
this.getLastIdInTable().then((lastIdInTableCurrent) => {
countOfAlreadyImportedRows = lastIdInTableCurrent -
lastIdInTableBeforeImport;
console.log("in loop");
console.log(countOfAlreadyImportedRows);
})
}else{
clearInterval(timerId);
}
}, 3000);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句