Is store.dispatch in Redux synchronous or asynchronous

陌路散爱 提交于 2019-11-26 07:44:21

问题


I realize this is a basic question but I had no luck finding the answer elsewhere.

Is store.dispatch synchronous or asynchronous in Redux ?

In case it is asynchronous is there a possibility to add a callback after the action has been propagated as it is possible with React ?


回答1:


AFAIK, dispatching action is synchronous. In case if you are willing to address the asynchronous call, you can use the thunk-middleware in redux, where dispatch is provided as a callback function which you can invoke as per your convenience. For more info, checkout this answer on SO by Author itself: How to dispatch a Redux action with a timeout?




回答2:


Nobody knows better than the code itself. =) As you can see dispatch is absolutely synchronous. The only warning here is that store enhancers can (and do) substitute dispatch method. For example, take a look at applyMiddleware enhancer, it lets you jack middlewares in by replacing default dispatch method with it's own implementation. Though I never saw any Redux enhancer which would actually remove synchronous nature of dispatch.



来源:https://stackoverflow.com/questions/43276291/is-store-dispatch-in-redux-synchronous-or-asynchronous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!