Giter Site home page Giter Site logo

Timeout feature about ofetch HOT 10 CLOSED

birdlavv avatar birdlavv commented on August 16, 2024 31
Timeout feature

from ofetch.

Comments (10)

nathanchase avatar nathanchase commented on August 16, 2024 11

Would very much like to see timeouts implemented - preferably with an exponential increase in timeout duration on subsequent retries. This is a much needed and essential feature coming from relying on it with axios.

Can we hook into the signal: AbortSignal.timeout(8000) mentioned here: whatwg/fetch#951 (comment)?

Something like:

$fetch(event.req.url, {
    baseURL: config.public.baseUrl,
    params,
    method,
    body,
    retry: 10,
    timeout: 2000, // signal: AbortSignal.timeout(2000),
    ...

from ofetch.

pi0 avatar pi0 commented on August 16, 2024 10

implemented in #268 (finally!)

from ofetch.

danielcmm avatar danielcmm commented on August 16, 2024 2

Very much needed!

from ofetch.

rmcmk avatar rmcmk commented on August 16, 2024 1

+1, would be a very useful feature to have. Currently have to hack around to get this to work in nuxt

from ofetch.

atinux avatar atinux commented on August 16, 2024

Some context: https://stackoverflow.com/questions/46946380/fetch-api-request-timeout

from ofetch.

atinux avatar atinux commented on August 16, 2024

from ofetch.

oleynikd avatar oleynikd commented on August 16, 2024

retryTimeout would be great too

from ofetch.

gustavopch avatar gustavopch commented on August 16, 2024

It could be implemented using https://npm.im/async-retry or adapted directly from the source code of https://github.com/tim-kos/node-retry to avoid adding a dependency.

EDIT: or perhaps just:

-  function onError(context: FetchContext): Promise<FetchResponse<any>> {
+  async function onError(context: FetchContext): Promise<FetchResponse<any>> {
     // Is Abort
     // If it is an active abort, it will not retry automatically.
     // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
     const isAbort =
       (context.error && context.error.name === "AbortError") || false;
     // Retry
     if (context.options.retry !== false && !isAbort) {
       let retries;
       if (typeof context.options.retry === "number") {
         retries = context.options.retry;
       } else {
         retries = isPayloadMethod(context.options.method) ? 0 : 1;
       }
 
       const responseCode = (context.response && context.response.status) || 500;
       if (retries > 0 && retryStatusCodes.has(responseCode)) {
+        context.options.timeout ??= 1000;
+        await new Promise(resolve => setTimeout(resolve, context.options.timeout));
+        context.options.timeout *= 2;
         return $fetchRaw(context.request, {
           ...context.options,
           retry: retries - 1,
         });
       }
     }

from ofetch.

Siddharth24Khera avatar Siddharth24Khera commented on August 16, 2024

+1, would be a very useful feature to have. Currently have to hack around to get this to work in nuxt

@rmcmk Can you share the workaround you are using in Nuxt.

from ofetch.

seth100 avatar seth100 commented on August 16, 2024

Looking forward to having this implemented !

from ofetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.