Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the jetpack domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/feedavenue.com/public_html/wp-includes/functions.php on line 6114
fetch with Timeout - Feedavenue
Friday, January 10, 2025
HomeTechnologySoftwarefetch with Timeout

fetch with Timeout

Date:

Related stories

Fun Tween Things

What would you add? By the wonderful Grace...

Marvel Rivals PC Graphics Settings Tips

Looking for the best settings to maximize performance...

Free Printable Sun Coloring Pages for Kids and Adults

Last updated: January 9, 2025 by the Homemade...

Kid Friendly Weekly Meal Plan

This website may contain affiliate links and advertising...
spot_imgspot_img

A few years back I wrote a blog post about how write a fetch Promise that times out. The function was effective but the code wasn’t great, mostly because AbortController , which allows you to cancel a fetch Promise, did not yet exist. With AbortController and AbortSignal available, let’s create a better JavaScript function for fetching with a timeout:

Much like the original function, we’ll use setTimeout to time to the cancellation but we’ll use the signal with the fetch request:

async function fetchWithTimeout(url, opts = , timeout = 5000) 
  // Create the AbortController instance, get AbortSignal
  const abortController = new AbortController();
  const  signal  = abortController;

  // Make the fetch request
  const _fetchPromise = fetch(url, 
    ...opts,
    signal,
  );

  // Start the timer
  const timer = setTimeout(() => abortController.abort(), timeout);

  // Await the fetch with a catch in case it's aborted which signals an error
  try 
    const result = await _fetchPromise;
    clearTimeout(timer);
    return result;
   catch (e) 
    clearTimeout(timer);
    throw e;
  
;

// Usage
try 
  const impatientFetch = await fetchWithTimeout('/', , 2000);

catch(e) 
  console.log("fetch possibly canceled!", e);

The JavaScript code above is much cleaner now that we have a proper API to cancel fetch Promise calls. Attaching the signal to the fetch request allows us to use a setTimeout with abort to cancel the request after a given amount of time.

It’s been excellent seeing AbortController, AbortSignal, and fetch evolve to make async requests more controllable without drastically changing the API.

  • 9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that…

  • Animated 3D Flipping Menu with CSS


Source link

Latest stories

spot_img