Sometime we need to abort last requests and call new one when we change route before requests finished. For this purpose you can use RequestsHandler from eventrix library. RequestsHandler gives you possibility to abort all of requests that you don’t need anymore.
Below we have example of simple request.
import axios from 'axios'; const getUsersList = () => { return axios.get('https://abc.com/users').then(({ data }) => { return data; }), } export default getUsersList;
Easy way to abort pending requests
We can have situation when we want to cancel last users GET request before call new one. For this purpose we can use code below.
import { RequestsHandler, Eventrix } from 'eventrix'; import axios from 'axios'; const eventrix = new Eventrix({}, []); const requestsHandler = new RequestsHandler(eventrix); const getUsersList = () => { const requestId = 'getUsers'; requestsHandler.abortAllById(requestId); return requestsHandler.handleRequest(axios.get('https://abc.com/users'), requestId).then(({ data }) => { return data; }), } export default getUsersList;
As we can see above, request is handled by RequestsHandler instance and we can abort all of requests with id “getUsers”. Of course RequestsHandler have more methods and we can abort all requests or resolve them with data what we expect.
Conclusion
Eventrix is library to manage global state of web and mobile applications based on React but has many cool features that give you easy way to resolve difficult problems. Thanks for reading and give me know what do you think about it.