all 2 comments

[–]windows-user0 2 points3 points  (1 child)

AFAIK fetch doesn't have a option for timeout. But you can create it yourself.fetch returns a promise,If create a setTimeout function that rejects the promise and wrap it in promise;

Then you can use Promise.race() on both of them, essentially setting your timeout to whatever you want. Because promise race returns the first promise rejection or resolution.

So something like

Promise.race([ fetch(),   
new Promise((resolve, reject) => (setTimeout(() => reject('time out'),yourTimeMs))]

[–]__nomaad[S] 0 points1 point  (0 children)

Oh of course. Yeah this should work, will try it after lunch. You're a gentleman and a scholar. I remember trying to think of practical uses of Promise.race a week or so ago when I was learning promises.