question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

any possible way to include timeout on fetch()?

See original GitHub issue

couldn’t find the docs for this repository… my code looks like this:

fetch('/test', { method: 'POST', body: data })
    .then(function(response) {
        if (response.status >= 400) {
            throw new Error("Bad response from server");
        }
        return response.json();
})
    .then(function(response) {
        console.log(response)
    });

would like to fake a server response for the fetch… any ideas?

thanks in advance

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:22
  • Comments:18

github_iconTop GitHub Comments

41reactions
tracker1commented, Jul 7, 2017

Create a timeoutPromise wrapper…

function timeoutPromise(timeout, err, promise) {
  return new Promise(function(resolve,reject) {
    promise.then(resolve,reject);
    setTimeout(reject.bind(null,err), timeout);
  });
}

You can then wrap any promise…

timeoutPromise(100, new Error('Timed Out!'), fetcn(...))
  .then(...)
  .catch(...)

It won’t actually cancel an underlying connection, but will allow you to timeout a promise.

27reactions
jimmywartingcommented, Apr 20, 2018

This is how you should be handling abort:

const controller = new AbortController()
const signal = controller.signal

setTimeout(() => { 
  controller.abort()
}, 1000)

fetch(url, { signal })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch API request timeout? - javascript - Stack Overflow
there's no timeout support in the fetch API yet. But it could be achieved by wrapping it in a promise.
Read more >
How to Set Timeout with the JavaScript Fetch API using ...
Use the setTimeout function to trigger the abort method after a specified time(convert to seconds by multiplying by 1000) and returns the ...
Read more >
Quickie fetch timeout | Ben Ilegbodu
I've always used the native Fetch API because it usually gives me everything I need without having to include another dependency. However, the ......
Read more >
How to timeout fetch requests in JavaScript - CodingDeft.Com
Adding timeout feature ... Now let's add the timeout feature to the loadData function. ... Here we are passing the timeout value as...
Read more >
JavaScript fetch with Timeout
const FETCH_TIMEOUT = 5000; let didTimeOut = false; new Promise(function(resolve, reject) { const timeout = setTimeout(function() { didTimeOut = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found