Can one use the Fetch API as a Request Interceptor?

后端 未结 2 1962
小蘑菇
小蘑菇 2021-01-18 04:57

I\'m trying to run some simple JS functions after every request to the server with the Fetch API. I\'ve searched for an answer to this question, but haven\'t found any, perh

2条回答
  •  渐次进展
    2021-01-18 05:24

    Just like you could overwrite the open method you can also overwrite the global fetch method with an intercepting one:

    fetch = (function (origFetch) {
        return function myFetch(req) {
            var result = origFetch.apply(this, arguments);
            result.then(someFunctionToDoSomething);
            return result; // or return the result of the `then` call
        };
    })(fetch);
    

提交回复
热议问题