Here's another synchronous task, which is a little bit complex, so you need some time to solve it
and you can't really solve it like in three or five minutes.
As you can see here, we must design a utility which takes a euro and a value for attempts which will
attempt to make fetch request.
If we have a failure, then this function will try again to fetch some data.
With increasing delay for number of times which user has requested, which actually means we must create
a function which will fetch a specific euro that we will provide.
And we also have some amount of attempts and we want to refresh this data again and again for several
times if we didn't get a success message.
And actually, this is a really common problem because sometimes our API is not working or data are
not there and we want to try it again.
So let's try and create this function, and they want to name it, for example, request management.
And actually, here we will get our URL because we want to fetch something from the outside and every
time something new.
And here we have some options for our George Howell.
And the last parameter will be attempts, and by default, it may be three, for example.
So we're trying to refresh some stuff three times.
And this is how we will use this.
So we have here request manager and are provided some URL, and they will use here some URL, which
does not exist.
In this case, we will refer to it.
So we have food dot com and this function must return for a promise.
This is why here we have then and here we have success and error.
This is why here we're waiting some response and we can just write.
Console.log, for example, responds to check if it's working.
So this is how we will typically use this function will want to fetch some URL and we simply have it
then.
And obviously, here we can pass some options, for example, method post and somebody or amount of
attempts if we want to change it.
If we didn't pass anything, then here we don't have any options.
And our attempt is three.
Now the question is what we want to return here and the most important point understand how we will
build it.
We will run this function recursively because this is an easy way to call this function several times.
And the idea is that we will call in the cage request manager again here, and we will simply provide
here inside your real options and then attempts.
We will decrease, which means at the beginning we have three attempts.
Now, the next time it will be two, then one, and then we will throw an error because we don't have
any remaining attempts.
So first of all, we must return here a promise because this is what we are waiting outside.
This is why here were returning your promise, and here we have our resolve and rechecked.
Now, inside this function, we want to try and fetch our data and we will simply use here for each
function because it is available for us natively and inside were passing the first to follow as usual.
And secondly, options.
And now here we have our then and we have catch and actually on.
Then we can simply call our resolve and this will pass the success data directly inside resolved because
it was always a function.
And here we also want to catch.
So here inside the cage, we want to do our magic.
So here we are getting our error and we want to call our request manager again.
But here we must know when a temp is the last one.
So here I want to create a property.
Is last a temp?
And here we can just check if our a temp equals one.
In this case of a note, OK, this is the last time when we can call our request manager, and after
this we are checking if this is a last attempt, then we want to return reject error, which actually
means we're just rejecting our promise because we can't continue anymore.
And after this, if we don't have is lust a temp, then we want to make that timeout because actually
we want to call this function only after some time.
It doesn't make any sense to call it directly after, because maybe API is still down.
So here I will write a timeout, for example, with three seconds.
And obviously, we can also implement an argument to past the interval to call data and insights at
timeout.
We want to request our data again.
So here Google calls request manager and we're passing here as well.
Now, options and the last one will be a temps minus one, which actually means we don't stop at temps
anywhere.
We simply provide recursively in request manager decreased amount of attempts.
This is why here on the first time it is three, then we'll come in here in a timeout.
It is two one and then we check this a temp equals one and we're hit inside thisif and were rejecting
it.
So let's check if it's working.
I'm reloading browser and as you can see, I get and get and an error because the year does not exist
and we're getting attempts is not define it on Line 13 inside our set timeout.
And actually, the name of that argument here is not correct.
It's not a temp, but attempts because we have several of them.
And also here it should be a temps equal one and not a temp.
Let's reload it again.
As you can see where get in, get terror.
Then after three seconds, we're getting this request again because we try it again and then again,
and here we haven't.
An error failed to fetch, which actually means here we must try it.
Not only then, but also catch because here we will get an error if we can't fetch data and here we
can just write console.log error.
And here we want to see our error.
But as you can see, we still get an error and code type error failed to fetch, which means our code
is not 100 percent correct.
And actually, the problem is here that this is our first call of request manager and we're handling
it outside with then and catch.
But here inserts a timeout.
We have request manager, but we don't have then.
And, Ketch, this is why it is breaking, so we have an error in our third call and this is here,
but we didn't specify what will happen, was it?
This is by whoever must try it then.
And here we want to resolve it with data and also catch and catch with simpler one to reject.
In this case, it will work correctly.
Let's try the the time to load in the page.
We're getting through errors and now our error is bubbled outside and we see it a pair of failed to
fetch.
So in this case, it is working correctly.
So you should not forget to babble our inner request manager outside with then and catch.
So I might say this function is quite complicated.
So if you receive something like this on interview, you really must take your time to solve it correctly.
You can't really design such function in a matter of minutes.