admin管理员组文章数量:1352865
I want to override the fetch()
method of isomorphic-fetch such that, just before sending this request it calls showLoader()
and after the request is finished, it calls hideLoader()
. Now I want to override it such that the new fetch can be chained just like the original fetch method. So it should respect/forward all the Promises. In summary, I want to:
- Override original
fetch()
intofetchPlus(extraArg)
fetchPlus()
needs to do addtional work just before callingfetch()
and just afterfetch()
pletes.fetchPlus()
should be a drop in replacement for usages of originalfetch()
- It should support chaining so that I can do:
fetchPlus(extraArg, originalArgs...).then(response => {...}).catch(error => {...});
I'm fairly new to JS, React and Redux so please excuse me if I missed something obvious. I did read up on fetch()
and know that it returns Promise so that the response can be passed on further or errors be handled. I sort of figured out an ugly way of doing it where, I pass on the responseHandler and errorHandler functions as args to fetchPlus.
fetchPlus(extraArg, url, options, responseHandler, errorHandler) {
showLoader(extraArg);
fetch(url, options)
.then(response => {
hideLoader(extraArg);
responseHandler(response);
}.catch(error => { errorHandler(error); });
}
But I don't find it clean. I want to see if I can find a drop-in replacement for the original fetch where Promises are correctly passed along.
I would highly appreciate any help!
I want to override the fetch()
method of isomorphic-fetch such that, just before sending this request it calls showLoader()
and after the request is finished, it calls hideLoader()
. Now I want to override it such that the new fetch can be chained just like the original fetch method. So it should respect/forward all the Promises. In summary, I want to:
- Override original
fetch()
intofetchPlus(extraArg)
fetchPlus()
needs to do addtional work just before callingfetch()
and just afterfetch()
pletes.fetchPlus()
should be a drop in replacement for usages of originalfetch()
- It should support chaining so that I can do:
fetchPlus(extraArg, originalArgs...).then(response => {...}).catch(error => {...});
I'm fairly new to JS, React and Redux so please excuse me if I missed something obvious. I did read up on fetch()
and know that it returns Promise so that the response can be passed on further or errors be handled. I sort of figured out an ugly way of doing it where, I pass on the responseHandler and errorHandler functions as args to fetchPlus.
fetchPlus(extraArg, url, options, responseHandler, errorHandler) {
showLoader(extraArg);
fetch(url, options)
.then(response => {
hideLoader(extraArg);
responseHandler(response);
}.catch(error => { errorHandler(error); });
}
But I don't find it clean. I want to see if I can find a drop-in replacement for the original fetch where Promises are correctly passed along.
I would highly appreciate any help!
Share Improve this question asked Sep 12, 2016 at 17:54 Manindra MoharanaManindra Moharana 1,0649 silver badges15 bronze badges 3-
Do you want to override the
fetch
function, or just to create a new function, which would be a modified version of thefetch
function? – Michał Perłakowski Commented Sep 12, 2016 at 18:28 - I want to wrap the fetch function so that it does additional things before and after calling the real fetch(). So I guess you can call it overriding fetch() or modified version of fetch()? The answer below solves my problem. – Manindra Moharana Commented Sep 12, 2016 at 18:49
-
Well, overriding would mean that you assign some other function to
fetch
. – Michał Perłakowski Commented Sep 12, 2016 at 18:51
1 Answer
Reset to default 7If you know that it returns a Promise, then why not just return?
fetchPlus(extraArg, url, options) {
showLoader(extraArg);
return fetch(url, options)
.then(response => {
hideLoader(extraArg);
return response;
})
}
Then use it:
fetchPlus(extraArg, url, options)
.then(response => ...)
.catch(error => ...)
本文标签: javascriptOverride fetch method in JS and forward PromisesStack Overflow
版权声明:本文标题:javascript - Override fetch method in JS and forward Promises - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743918958a2561728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论