How to call a specific function before and after every fetch api request and response Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to call a specific function before and after every fetch api request and response without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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.

Answer

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.

We are here to answer your question about How to call a specific function before and after every fetch api request and response - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji