W
W
webe2018-08-29 02:48:35
React
webe, 2018-08-29 02:48:35

How do Thunks work?

const addPost = () => (dispatch) => {
  dispatch({ type: ADD_POST}); 
  dispatch({ type: SEND_MESSAGE }); 
  dispatch({ type: SEND_LOG }); 
};

A little confused with the concept of asynchronous sledges.
Do I understand correctly that this code will be executed synchronously
i.e. will it always be in the order in which it is described in my example?
(in this example, we do not use AJAX, we just work with data inside the editor)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-08-29
@webe

How do Thunks work?

Did you mean async actions and redux-thunk middleware ?
Correctly.
The store.dispatch() call itself is synchronous. But if not an object is passed there, but a function such as addPost, then it does not reach the reducers. It is intercepted by the redux-thunk middleware and executed by passing dispatch, getStore and an optional third argument there. It is clear that such a function can be asynchronous.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question