Sorry to be so incredibly basic but what exactly does asynchronous endpoint mean?
My initial understanding of asynchronous was in the context of setState() in REDUX. There, I understood that states might get updated as a batch. That is, a bunch of state updates are collected and then updated at the same time. Rather than in the order which the logic of code might have dictated.
If that is an accurate understanding, then does it follow that an asynchronous endpoint mean that data from somewhere is collected and sent over in a batch…? And that the implication of this is that you need to present something on the screen so user doesn’t think the app has died…?
Asynchronous in that you send a request to get something, and at some point later on, a response comes back. Meanwhile, everything else carries in normally. This is as opposed to synchronous, when everything would stop until that response comes back.
Out of the box, Redux does everything synchronously, which is a little bit of a problem when making HTTP requests in particular, but one that is very easy to solve.
So if you go and GET some data from an API. In Redux:
You dispatch an action saying you’ve made the request.
You’d like that action to just update the state based on the response, but you can’t do that, because you’d have to wait for it.
So that action just updates the state to say you’ve made the request.
And at this point you have a problem: the action updates the state, but it ain’t going to do anything else. The payload has been delivered, nothing else ever happens.
What you probably want to do is dispatch the action, then that action does something that sends the request, and then that fires another action once the request completes/fails. You’re using thunks here to do that: if you wrap the action creator in another function, it won’t fire until you execute that outer function (the thunk), so basically you can do some other stuff before triggering it.