Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeOut request #26

Open
AndreaMinato opened this issue Sep 12, 2018 · 2 comments
Open

TimeOut request #26

AndreaMinato opened this issue Sep 12, 2018 · 2 comments

Comments

@AndreaMinato
Copy link
Contributor

How do I set a timeout for a request?

As I read in the docs i should accomplish that setting the timeout option like this

const rootService = new ApiService({
  adapter: fetch,
  mode: "cors",
  options: { timeout: 20000 },
  hooks: {
    before({ payload, meta, next }) {
      if (meta.guest) return next(payload);
      else {
        const token = store.state.auth.token;
        next(withToken(token, payload));
      }
    }
  }
});
rootService.on("fail", e => {
  if (e.status === 403) {
    store.dispatch("auth/unauthorized", e.body);
  }
  console.log({ status: "fail", obj: e });
});
rootService.on("error", e => {
  store.dispatch("handleAppError", e.body);
  console.log({ status: "error", obj: e });
});

But doing like this I end up with this error

[Apicase.deprecated] Delayed requests and features of Requests+ are now deprecated. Use https://github.com/apicase/spawner instead apicase.js:141:6
------
TypeError: 'abort' called on an object that does not implement interface AbortController.[Ulteriori informazioni] apicase.js:205:31
    cancel apicase.js:205:31
    start/< apicase.js:197:29

So i believe that I shoul use the spawner but I can't find in the docs how to set a timeout with the spawner, also i don't have the time to rewrite all the infrastructure using spawners just to set a timeout in the base service

As here it seems to be a todo. What may I do?

@Kelin2025
Copy link
Member

Kelin2025 commented Sep 13, 2018

Hi @AndreaMinato. Sorry for the delay, I'll try to find some time to complete spawner.
As a temporary solution you can do something like

// Wrapper function
const doLimitedRequest = (Service, time, payload) => {
  const req = Service.doRequest(payload)

  const timer = setTimeout(() => {
    req.cancel()
  }, time)

  req.on('finish', () => {
    clearTimeout(timer)
  })
}

doLimitedRequest(Service, 1000, { url: 'test' })

Btw, this is a strange behaviour, I'll check it on weekends

TypeError: 'abort' called on an object that does not implement interface AbortController.[Ulteriori informazioni] apicase.js:205:31
    cancel apicase.js:205:31
    start/< apicase.js:197:29

@SpurguX
Copy link

SpurguX commented Mar 5, 2020

I attempted to implement a timeout wrapper as per your suggestion but calling cancel() on the request (still) throws that TypeError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants