Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

clean up async requests in react useEffect hook using abort controller

useEffect(() => {
    const controller = new AbortController();
    const { signal } = controller;
    async function fetchData() {
      try {
        let response = await fetch(`https://example.com/${x}`, { signal });
        if (response.ok) {
          let data = await response.json();
          console.log(data);
        }
      } catch (error) {
        console.log(error);
      }
    }
    fetchData();

    return () => {
      controller.abort();
    };
  }, []);
Comment

PREVIOUS NEXT
Code Example
Javascript :: connect vite with node js 
Javascript :: terminal text length nodejs 
Javascript :: subtract 18 years from today javascript 
Javascript :: remove attribute disabled 
Javascript :: p5.js script tag 
Javascript :: convert text to binary javascript 
Javascript :: ajax call with form data 
Javascript :: js change div content 
Javascript :: socket io https 
Javascript :: javascript date 3 months ago 
Javascript :: how to set header in angular 8post 
Javascript :: how to prepare key in object dyamically javascript 
Javascript :: javascript find link by href 
Javascript :: reapeat until in js 
Javascript :: Twilio room does not disconnect / Webcam LED remains on 
Javascript :: console message inspector javascript 
Javascript :: jq count outputs 
Javascript :: how to add an element to the last position of an array in javascript 
Javascript :: how to remove angular package 
Javascript :: jquery if input has empty white space 
Javascript :: json datetime 
Javascript :: how to give a label padding through jquery 
Javascript :: how to get the contract address from the contract instance web3js 
Javascript :: how to get cwd nodejs 
Javascript :: js add to local storage 
Javascript :: how to change the font family using jquery 
Javascript :: json enconde 
Javascript :: javascript variable shortcuts 
Javascript :: how remove child in jquery 
Javascript :: jquery clone and append 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =