中止 OLD API Fetch 请求 REACT

Aborting OLD API Fetch request REACT

提问人:Sanman Chavan 提问时间:3/2/2021 更新时间:3/2/2021 访问量:914

问:

在我的申请中, 有一个文本框,如果用户输入任何位置,则应用程序将搜索该位置并创建 5 个间隔为 5 秒的提取 API 请求

if requestId :100 then requests will be ==> 
1. API/GetResult?rv=0.5936085871062164 &  jsonBodyHotel :100      //this is completed
2. API/GetResult?rv=0.5936085871062165 &  jsonBodyHotel :100      //this is completed
3. API/GetResult?rv=0.5936085871062166 &  jsonBodyHotel :100      //this is pending 
4. API/GetResult?rv=0.5936085871062167 &  jsonBodyHotel :100      //this is pending 
5. API/GetResult?rv=0.5936085871062168 &  jsonBodyHotel :100      //this is pending 


Now before completeting those 5 request,if user enters new location ,then again application create 5 fetch API request
for new requestId :101
1. API/GetResult?rv=0.5936085871062174 &  jsonBodyHotel :101     
2. API/GetResult?rv=0.5936085871062175 &  jsonBodyHotel :101     
3. API/GetResult?rv=0.5936085871062176 &  jsonBodyHotel :101     
4. API/GetResult?rv=0.5936085871062177 &  jsonBodyHotel :101     
5. API/GetResult?rv=0.5936085871062178 &  jsonBodyHotel :101     

Once the above process starts ,we need to abort all API with old requestId,
here for an instance ,
3. API/GetResult?rv=0.5936085871062166 &  jsonBodyHotel :100      //this needs to be aborted 
4. API/GetResult?rv=0.5936085871062167 &  jsonBodyHotel :100      //this needs to be aborted      
5. API/GetResult?rv=0.5936085871062168 &  jsonBodyHotel :100      //this needs to be aborted 

and so on for every new request .......

我创建的内容如下,我在控制台中没有收到任何错误,但也无法中止旧请求

import AbortController from "abort-controller"

class ClassContainer extends React.Component {
    constructor(props) {
        super(props);
        this.controller = new AbortController();
        this.signal = this.controller.signal;
    }
    
    


//  Below is the function gets executed whenevr user enter any new location , show loading indictaor and creates 5 fetch API request
searchTravel=()=>{
 this.controller.abort();
 //show loader 
 //API hit to get new requestId
 this.getData(requestId)
}



//  This is a function which will create 5 fetch API request for every requestId==>
getData=(requestId)=>{
jsonBodyHotel : will have requestId & other stuff 

  const response = fetch("/API/GetResult" + "?rv=" + Math.random(), {
          method: "POST",
      body: jsonBodyHotel,
      headers: {
        "Content-type": "application/json; charset=UTF-8",
      },
    },this.signal)
}

如何中止旧请求,每次生成新请求时

reactjs fetch 中止

评论


答:

1赞 nishit chittora 3/2/2021 #1

有一个问题为什么它不起作用。您需要为创建到后端服务器的每个新请求进行创建。new AbortController

import AbortController from "abort-controller"

class ClassContainer extends React.Component {
    constructor(props) {
        super(props);
    }
    
    


//  Below is the function gets executed whenevr user enter any new location , show loading indictaor and creates 5 fetch API request
searchTravel=()=>{
 if (this.state.controller) {
       this.state.controller.abort();
 }
 
const controller = new AbortController();
const signal = controller.signal;

this.setState({
  controller: controller,
  signal: signal
})

 //show loader 
 //API hit to get new requestId
 this.getData(requestId)
}



//  This is a function which will create 5 fetch API request for every requestId==>
getData=(requestId)=>{
jsonBodyHotel : will have requestId & other stuff 

  const response = fetch("/API/GetResult" + "?rv=" + Math.random(), {
          method: "POST",
      body: jsonBodyHotel,
      headers: {
        "Content-type": "application/json; charset=UTF-8",
      },
    },this.state.signal)
}

评论

0赞 Sanman Chavan 3/2/2021
Nishit ,我仍然无法中止旧请求,我所做的更改:1.中止状态控制器和每个新请求的新中止实例 2.在调用 getData() 之前维护状态 3.在 GetResult API 中使用 state.signal
0赞 Sanman Chavan 3/3/2021
Nishit ,能够在一些逻辑更改后中止请求,但现在我想在捕获中捕获那些中止的请求,我正在更新问题
0赞 Sanman Chavan 3/3/2021
我创建了一个新问题,下面是链接 stackoverflow.com/questions/66454690/...