This post is republished since the previous one was not showing up on my profile for some reason.
So recently, I bumped into a situation where t...
For further actions, you may consider blocking this person and/or reporting abuse
Liquid syntax error: 'raw' tag was never closed
Perhaps add a check for a value smaller than 1 to avoid the infinite cycle:
Thanks! Your fetch_retry function saved the day on a bug I was encountering in deno:
github.com/denoland/deno/issues/13...
If someone in 2021 trying the Primitive version or ES6 version, for some reason the execution never gets to catch unless you add the processing of the success before it: so instead of
return fetch(url, options).catch(function(error)
you absolutely necessarily need to do
return fetch(url, options).then(res => res.json()).catch(function(error)
or whatever processing of response you want to do in that .then()
you can still do next .then down the line from what fetch_retry returns you afterwards
But without this then fetch_retry doesn't actually attempt any retries as it never gets to catch, even though it got 500 server error in my case.
This is a very useful and perfectly written post. It not only provides an end result but also teaches you by showing the steps one would follow to write this function and improve it after some iterations, so you learn a way of thinking that can be applied to other functions you may want to write. Thank you for the post!
I gave it a swing with deno and came up with this:
The short version of it
So usefull, thanks to share this
Thanks Jason. That's my version based on your example:
Then I can call:
fetchWithRetry(
api/data/${guid}
).then(requestProcess)Hi, can I add the retries to the options?
fetch(url, {
retries: 3,
retryDelay: 1000
})
.then(function(response) {
return response.json();
})
.then(function(json) {
// do something with the result
console.log(json);
});
Hello, I am a beginner of javascript . After watching your blog about promise ,especially this part-->
.....
.catch(function(error) {
fetch_retry(url, options, n - 1)
.then(resolve) // <--- simply resolve
.catch(reject); // <--- simply reject
})
I don't understand why resolve & reject can be included in then & catch functions directly... Is anything omitted?
Could you tell me why or where I can reference about this issue?
Sorry for my poor English.
I truely appreciate for your time.
Thanks for your comments! I am not sure if I understand your question correctly.
But it sounds like you are confused with the idea of "functions as first-class citizens", which is a fancy way of saying you can pass functions as a values around.
Are you familiar with
setTimeout
? Have a read about it here.Consider
We are passing a function
function () { print('hello'); }
tosetTimeout
as the first argument. So if we call that functionf
, then we can equivilantly passf
tosetTimeout
.So in another words, my code:
Is basically equivalent to:
It is also important to note that the following is incorrect (or not the same):
Because Javascript will first evaluate
resolve()
and use the value it returns to pass on tothen
.I hope I did help a little.
In the last line, do we really need to return
await ...
? i.e.return await fetch_retry(url, options, n - 1);
?
Can we simply
return fetch_retry(url, options, n - 1);
?
You can omit the await. I like putting await because it reminds me that I am dealing with async functions and direct values instead of Promises. Although async/await are just playing with promises at the end of the day.
Sorry about the issue. We know all about what the problem was. Slighly less about what the solution is yet. We'll have it fixed soon, promise.
It's alright, copy and pasting isn't very hard. Only thing is that I lost my comments and likes. I have set my previous post to unpublished in case you guys fixed it and two articles show on my profile. haha