链接redux-actions和redux-promise-middleware

时间:2016-12-28 17:33:38

标签: typescript redux redux-promise

我使用redux-actionsredux-promise-middleware来发送操作,以及async await支持的TypeScript redux-actions

这是同时使用redux-promise-middleware// create an async action const fooAction = createAction('FOO', async () => { const { response } = await asyncFoo(); return response; }); // use async action fooAction('123')

的操作
redux-promise-middleware

这是动作链的一个示例,仅使用const foo = () => dispatch => { return dispatch({ type: 'TYPE', payload: new Promise() }) .then(() => dispatch(bar())); }

redux-promise-middleware

redux-actions中的链接如何与ind = bsxfun(@plus, 0:numel(f)-numel(g), (1:numel(g)).'); x = prod(bsxfun(@times, f(ind), g(:)), 1); 一起使用?

2 个答案:

答案 0 :(得分:3)

您必须记住,即使async await看起来是同步的,它也会使用Promise,而async函数将始终返回Promise,无论您使用{{1}或不。

由于await的第二个参数是您的有效负载创建者,因此没有任何东西可以阻止您使用生成的对象。

以下是基于初始代码的示例:

createAction

答案 1 :(得分:0)

Aperçu回答的问题是“等待”你是阻止事件循环而你必须直接处理Promises。

还有“redux-promise-middleware”的替代方案,redux-auto与redux-promise-middleware具有相同的API,但也提供了一种链接reducers调用的机制。

您的示例如下所示:

// UI code
actions.data.foo()

// store/data/foo.js
export function fulfillment(data,payload){
   return data
} fulfillment.chain = actions.x.bar

export function action(payload){
    return Promise.resolve()
}

真的,就是这样。您只需要将操作分配给链属性,而redux-auto将在redux生命周期的正确位置调用它

  

了解上述来源。 redux-auto会根据文件结构自动创建操作并将它们连接起来。文件夹名称成为状态属性的名称。文件夹中的文件是要在该州的该部分执行的操作。

以下是文档chaining action together

相关问题