Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
How to call a specific function before and after every fetch api request and response

I want to call a function during sending a request and after receiving response with every fetch() function using Vanilla JS. I saw that jquery ajax is providing some kind of $.ajaxsetup for doing this but I want to do it with Vanilla JS. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

fetch doesn't have a feature like that. You could write a wrapper function though:

const myFetch = async (...args) => {
    do_before();
    const result = await fetch(...args);
    do_after();
    return result;
}

and then call that function instead of accessing fetch directly.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it by overriding the fetch function

// First rename original fetch function
window.originalFetch = window.fetch

// Then override fetch function with your new function
window.fetch = async (... args) => {
    console.log("before");
    // call the renamed fetch function
    const result = await window.originalFetch(...args);
    console.log("after");
    return result;
}


// Then you can use it
fetch('http://example.com/movies.json')
  .then(response => response.json())
  .then(data => console.log(data));

Explanation:

First, you change the original function name, then you write your own function with the same name "fetch"

Thanks to @Quentin some of the code copied from his answer

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda