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

added timeout support #435

Closed
wants to merge 2 commits into from
Closed

added timeout support #435

wants to merge 2 commits into from

Conversation

Gcaufy
Copy link

@Gcaufy Gcaufy commented Nov 16, 2016

it's really useful to add a timeout option.

@mislav
Copy link
Contributor

mislav commented Nov 16, 2016

Thanks, but we can't accept a feature that's not in the spec! See https://github.com/github/fetch#read-this-first and #175 (comment) #405 #246 #181 #68 #16 for background.

@mislav mislav closed this Nov 16, 2016
@Gcaufy
Copy link
Author

Gcaufy commented Nov 17, 2016

ok, got it.

@fritx
Copy link

fritx commented Jan 12, 2018

Excuse me, how could we increase timeout to Infinity or over 120s which is default value of Chrome?

I couldn't find out a solution from those issues :/


Do I have to switch to axios or other libs to do the fetch?

PS: whatwg/fetch#20 (comment)

@Gcaufy
Copy link
Author

Gcaufy commented Jan 12, 2018

@fritx
you can use Promise.race instead.

const fetchTimeout = (p, time) => {
    let timeout = new Promise((resolve, reject) => { 
        setTimeout(() => reject(new Error('timeout')), time); 
    });
    return Promise.race([p, timeout]);
};


fetchTimeout(fetch({}), 3000).then()

@fritx
Copy link

fritx commented Jan 12, 2018

@Gcaufy your code looks like to declare a timeout instead, but I want to drop any timeout that would exist. Am I missing something?

@Gcaufy
Copy link
Author

Gcaufy commented Jan 12, 2018

this is like

axios({timeout: 3000})

so I guess what you need is

axios.defaults.timeout = 3000

or a common call back, like:

fetch.globalTimeoutCallback = () => {
   console.log('this is timeout');
}

what ever you need, you can use Promise.race get what you want:
e.g.

const reqest = (p, time = null) => {
   time = time || fetch.defaults.timeout;
   if (!time) return p;
   let timer;
   let timePromise = new Promise((resolve, reject) => {
     timer = setTimeout(() => {
        fetch.globalTimeoutCallback();
        reject(new Error('timeout'));
    }, time)
   });
   return Promise.race([p.then(res => {
        clearInterval(timer);
        return res;
    ), timer])
}

@jimmywarting
Copy link

actually, this is what you should be doing:

const controller = new AbortController()
const signal = controller.signal
setTimeout(() => { 
  controller.abort()
}, 1000)

fetch(url, { signal })

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants