Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to delay api call?

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));,

like image 230
Italik Avatar asked Dec 17 '20 09:12

Italik


People also ask

Can we call a API in setTimeout Javascript?

Using setTimeout() We can pass an API call inside the setTimeout() to execute it after a certain delay.

Why does my API call take so long?

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.

How to delay a web query before calling it?

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

What is the best way to handle slow API responses?

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.

What are the best practices for API rate limiting?

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 ...

What are API calls and how do they work?

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.


Video Answer


2 Answers

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);
}
like image 97
AKX Avatar answered Oct 16 '22 18:10

AKX


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!

like image 28
Mario Petrovic Avatar answered Oct 16 '22 20:10

Mario Petrovic