Javascript Redux 中的 store.dispatch 是同步还是异步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43276291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is store.dispatch in Redux synchronous or asynchronous
提问by ps-aux
I realize this is a basic question but I had no luck finding the answer elsewhere.
我意识到这是一个基本问题,但我没有运气在其他地方找到答案。
Is store.dispatchsynchronous or asynchronous in Redux?
是store.dispatch同步的还是异步的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?
如果它是异步的,是否有可能在传播动作后添加回调,因为它可能与React?
采纳答案by agpt
AFAIK, dispatching action is synchronous. In case if you are willing to address the asynchronous call, you can use the thunk-middlewarein 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?
AFAIK,调度动作是同步的。如果您愿意处理异步调用,您可以使用redux 中的thunk-middleware,其中 dispatch 作为回调函数提供,您可以根据自己的方便调用。有关更多信息,请查看作者自己在 SO 上的答案:How to dispatch a Redux action with a timeout?
回答by fkulikov
Nobody knows better than the code itself. =) As you can see dispatchis absolutely synchronous. The only warning here is that store enhancerscan (and do) substitute dispatchmethod. For example, take a look at applyMiddlewareenhancer, it lets you Hyman middlewares in by replacing default dispatchmethod with it's own implementation. Though I never saw any Redux enhancerwhich would actually remove synchronous nature of dispatch.
没有人比代码本身更了解。=) 如您所见,dispatch是绝对同步的。这里唯一的警告是 storeenhancers可以(并且确实)替代dispatch方法。例如,看看在applyMiddleware增强,它可以让你在更换默认插孔中间件dispatch与它自己的实现方法。虽然我从未见过任何 Reduxenhancer实际上会删除dispatch.

