Invariably the question that comes up when talking about Redux is how does one handle asynchronous operations in redux. For instance, how do we hand off an operation to redux that requires a remote call to the server? Where exactly does the async part get handled?

Redux has this concept of middleware that allows us to insert custom logic in the space between dispatching an action, and the moment it reaches the reducer. In this lesson, we are going to set up a few async operations and then use redux thunk middleware to make sure that everything makes it into the reducer properly.

The main key to thunk middleware is that it allows us to return a function instead of an action. This function can encapsulate the async operation and dispatches the appropriate action when the operation is completed.

To handle async opreations, we can install:

npm install --save redux-thunk

Import:

import thunk from 'redux-thunk';

Use it as middle ware:

const config = $ngReduxProvider => {
'ngInject';
$ngReduxProvider.createStoreWith(rootReducers, [thunk]);
};

Refactor action creator to use thunk middleware:

export const CategoriesActions = ($http, $q) => {
'ngInject';
const FETCH = {
categories: 'data/categories.json'
}; /*const getCategoreis = (categories) => {
return {type: GET_CATEGORIES, payload: categories}
};*/ const getCategoreis = () => {
return (dispatch, getState) => {
const { categories } = getState();
if (categories.length > ) {
return $q.when(categories);
} else {
return $http.get(FETCH.categories)
.then(res => res.data)
.then((payload) => {
return dispatch({
type: GET_CATEGORIES,
payload
})
});
}
};
}; ....
}

Here we use cache, so we need to set initial value to empty array:

export const categories = (state = [], { type, payload }) => {
switch (type) {
case GET_CATEGORIES:
return payload || state;
default:
return state;
}
};

[AngularJS NG-redux] Handle Asynchronous Operations with Middleware的更多相关文章

  1. .NET:CLR via C# Compute-Bound Asynchronous Operations

    线程槽 使用线程池了以后就不要使用线程槽了,当线程池执行完调度任务后,线程槽的数据还在. 测试代码 using System; using System.Collections.Generic; us ...

  2. A filter or servlet of the current chain does not support asynchronous operations. 错误解决记录

    做视频文件上传一直报这个错误: java.lang.IllegalStateException: A filter or servlet of the current chain does not s ...

  3. redux深入理解之中间件(middleware)

    理解reduce函数 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. arr.reduce([callback, initi ...

  4. How does a single thread handle asynchronous code in JavaScript?

    原文:https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript ----------- ...

  5. Part 6 AngularJS ng repeat directive

    ng-repeat is similar to foreach loop in C#. Let us understand this with an example. Here is what we ...

  6. part 4 AngularJS ng src directive

  7. Part 15 AngularJS ng init directive

    The ng-init directive allows you to evaluate an expression in the current scope.  In the following e ...

  8. 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)

    原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...

  9. 通过.NET客户端调用Web API(C#)

    3.2 Calling a Web API From a .NET Client (C#) 3.2 通过.NET客户端调用Web API(C#) 本文引自:http://www.asp.net/web ...

随机推荐

  1. C++的class的样例

    私有就是仅仅可以通过内部调用,在类外面是不可以使用私有成员的 简单的写一个  Class A {  public:    //你能够通过公有的函数去訪问私有成员    Demo()   //能够在这使 ...

  2. android-LinearLayout 控件占满父容器位置实现

    经常碰到需要把一个控件放在手机底部的情况,以前都是在LinearLayout尝试使用gravity="bottom" ,但是,没有效果,后来在网上查到了方法,如下 <Line ...

  3. HDU 1548 A strange lift(最短路&&bfs)

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. element-UI实现el-table-column百分比自定义分配

    1.把el-table-column的属性width换位min-width就支持百分比显示了.

  5. python处理文件

    打开文件:     open是内建函数,一个方法 open("test.txt","r",buffering=1) test.txt 表示被打开的文件名,如果不 ...

  6. 王小川分享AI

    王小川的分享:Link

  7. fromCharCode vs chr

    fromCharCode vs chr echo off set "fn=%*" set php=d:/www/php5/php.exe cls echo. %php% %fn% ...

  8. 前端面试题(HTML/CSS)

    (前端面试题大全,持续更新) 常用的块级元素和行内元素有哪些?说说他们的特点? 浮动产生的原因?清除浮动? 说说一下盒模型 float和position一起用是什么效果 rem用过吗?做不同手机的适配 ...

  9. 【例题 7-13 UVA-1374】Power Calculus

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 结论:每次只用新生成的数字就好了. 然后就是IDA*了. 迭代深搜+剪枝. [代码] /* 1.Shoud it use long ...

  10. ArcGIS在线帮助的使用指南

    一直感觉ArcGIS的在线帮助就是鸡肋,没想到网络常见的所谓的高大上的博文,也不过是对GIS 在线帮助的拷贝,或者简单修改而已.其实ArcGIS的在线帮助包含了以下几个很好用的模块: 备注 ArcGI ...