viniciusdacal/redux-arc

support for cancellable requests?

Closed this issue · 1 comments

jvega commented
support for cancellable requests?

That depends on the lib you are using to perform the request.
let's consider this following example:

const asyncTask = store => done => (options) => {
  const { method, url, payload } = options;
  const params = method === 'get' ? { params: payload } : payload;
  axios[method](url, params).then((error, response) => done(error, response));
};

If you return some value here, the value will be received from where you dispatched the request action. Let's say you want to support promisses, you could return the result of axios, like this:

const asyncTask = store => done => (options) => {
  const { method, url, payload } = options;
  const params = method === 'get' ? { params: payload } : payload;
  return axios[method](url, params).then((error, response) => done(error, response));
};

And then, you could use the promise like this:

dipatch(creators.list()).then((response) =>) .....

So, if your lib supports something like promise.abort()

You could do the following:

const promise = dipatch(creators.list());
// ....
promise.abort()

In the case of axios, you would need to return the cancellation token

https://github.com/mzabriskie/axios#cancellation