Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
ReactJS fetch call with AbortController

I am trying to cancel the fetch calls using the AbortController. This is what my API extraction looks like. I have a function that handles both get and post calls and these get and post methods and extracted out separately as well.

export async function customAjax(options){
   let options = {};
   options.headers = { ...options.headers, ...obj.headers };
   const response = await fetch(url, options);
   await response.json()
}

const get = (url, extra = {}) => api({ url, type: "GET", ...extra });
const post = (url, payload, extra = {}) => api({ url, data: payload ,type: "POST",
}, ...extra });

const api = async (payload) => {
      try {
          promise = customAjax(payload);
          const response = await promise;
          if (response && !errorMessage) {
            return { response, error: false, loading: false, request: promise };
          } else if (errorMessage) {
            return { response: false, error: "Error", loading: false, request: promise };
          }
      } 
     catch (e) {
         return { response: false, error: "Error", loading: false, request: promise };
     }
    return { response: false, error: false, loading: true, request: promise };
}

So from respective components, I will be calling get or post methods.

function MyComponent(){
  useEffect(() => {
    makeCall();
    saveResponse();
  }, []);

 async function makeCall(){
   const { response, error } = await post(URL, payload);
   // Handling code is not added here
 }

  async function saveResponse(){
   const { response, error } = await get(URL);
   // Handling code is not added here
  }
}


As you see this is a component where I call both the methods. I basically want to cancel the calls when moving away. What is ideal way to create a abort instance and pass it? How do I achieve with my current API structure. Some pointers would help and appreciated

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda