In my app (React + Redux + axios + redux-thunk + Typescript) I have a situation when I do API call, change item in database and show current, changed state in table. So, it looks like:
const changeItem = () => changeItemState(item.id).then(getItem(item.id));
Both actions (changeItemState and getItem) are imported from reducer file.
The problem is, changing item process, on the back-end site, takes too long. So, I reload table (call api to get item - getItem(item.id)
) but the backend changing process is still ongoing, so I receive old state. After 100-300 milliseconds I can query item again (refresh page for example) and get new state of item. But I don't want to do it manually. I want to reload table after back-end prepared my item to show with new state.
The question is - how to delay get item action? Are there any front-end patterns for this? I would like to avoid use “setTimeout()” method like this (imo it’s not an ideal solution):
const changeItem = () => changeItemState(item.id).then(setTimeout(function(){ getItem(item.id); }, 300));
,
Using setTimeout() We can pass an API call inside the setTimeout() to execute it after a certain delay.
The larger the database (more data concerning devices, users, etc.), the longer it will take to sort through all the data and return a result. This also correlates to the back-end where calls are processed on a server.
you can add a delay with the Power Query function Function.InvokeAfter(). Check out the tutorial of Chris Webb: Chris Webb's BI Blog: Using Function.InvokeAfter() In Power Query Chris Webb's BI Blog (crossjoin.co... Or we had the same question already in the community: Solved: Re: Delay a web query before calling it - Microsoft Power BI Community
The less “global” state you maintain, the better. It not only helps keep your code simple and modular, but it also gives you more freedom to increase concurrency. A better approach, in this case, is to not deal with the response coming out of a slow API but to simply stop receiving responses from it.
Best Practices For API Rate Limiting 1 Are requests throttled when they exceed the limit? 2 Do new calls and requests incur additional fees? 3 Do new calls and requests receive a particular error code and, if so, which one? More ...
That’s where API calls come in. What is an API Call? An API call is the process of a client application submitting a request to an API and that API retrieving the requested data from the external server or program and delivering it back to the client. Let’s say your app uses Facebook APIs to extract data and functionality from the platform.
You can promisify setTimeout
like this:
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
Then,
const changeItem = () =>
changeItemState(item.id)
.then(() => delay(300))
.then(() => getItem(item.id));
or equivalently with an async function
const changeItem = async () => {
await changeItemState(item.id);
await delay(item.id);
return getItem(item.id);
}
Well, there is no elegant solution if you are not using Websocket (https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
The only way you can is to have those setTimeout
delays and compare old data with new. That is the best you can do.
But if you go with Websockets route, you can have realtime communication from BE and it will emit changes and you app will react to those changes.
This will require you to do refactoring on BE to support Websockets.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With