[AngularJS NG-redux] Handle Asynchronous Operations with Middleware
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的更多相关文章
- .NET:CLR via C# Compute-Bound Asynchronous Operations
线程槽 使用线程池了以后就不要使用线程槽了,当线程池执行完调度任务后,线程槽的数据还在. 测试代码 using System; using System.Collections.Generic; us ...
- 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 ...
- redux深入理解之中间件(middleware)
理解reduce函数 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. arr.reduce([callback, initi ...
- How does a single thread handle asynchronous code in JavaScript?
原文:https://www.quora.com/How-does-a-single-thread-handle-asynchronous-code-in-JavaScript ----------- ...
- 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 ...
- part 4 AngularJS ng src directive
- Part 15 AngularJS ng init directive
The ng-init directive allows you to evaluate an expression in the current scope. In the following e ...
- 【ASP.NET Web API教程】3.2 通过.NET客户端调用Web API(C#)
原文:[ASP.NET Web API教程]3.2 通过.NET客户端调用Web API(C#) 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的 ...
- 通过.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 ...
随机推荐
- vue 点击事件阻止冒泡 用stop
1.使用vue阻止子级元素的click事件冒泡,很简单,用stop.eg: @click.stop='xxx'
- excel的隔行插入
https://wenda.so.com/q/1523455238213064 #公式 IF(ISODD(ROW()),OFFSET($B$1,INT((ROW(A1)-1)/2),),OFFSET( ...
- Python 3 下载安装和环境搭建
Python3 下载 由于博主使用的平台是Windows10,以下方法仅限win10系统 Python 官网:https://www.python.org/ 找到跟系统相应的版本瞎子: Python ...
- 摆脱技术思维,转向产品思维——寻找“万能”IDC的苦恼
背景:近期在新产品的开发任务完毕后一直在为寻找好的IDC和优质的托管服务忙碌.需求源自于我们重点要解决之前老版产品面临的国内外用户訪问速度慢甚至连接不上的问题. 除去架构技术上使用高性能.可扩展的方案 ...
- 结构体类型重声明导致的bug一个
bug前提条件 当模块比較多.头文件较多,某个结构体类型会在当前模块中又一次声明进而引用其成员,而不直接包括其它模块的头文件. 这种优点是不引入不须要的类型声明到此模块.头文件包括的交叉:坏处是,添加 ...
- Xcode 6 打包ipa文件
随着Xcode6.1的普遍应用.随之而来的非常多问题就会出现.这里来说一下怎样在Xcode6.1上生成Ad-hoc ipa.首先是要在你的开发人员账号上生成一个.ipa的主要应用就是在你公布到AppS ...
- 任务调度(四)——ScheduledExecutorService替代Timer,实现多线程任务调度
上篇博文<任务调度(三)--Timer的替代品ScheduledExecutorService简介>已经对ScheduledExecutorService做了简介.事实上使用Schedul ...
- js39---组合模式,查找遍历树
/** *有这样一个需求 *有一个学校有2个班(一班,二班) *每个班级分2个小组(一班一组,一班二组,二班一组,二班二组) *学校计算机教室有限,每一个小组分着来上课. *考试的时候大家一起考 *请 ...
- screen-Orientation 横竖屏设置
1.xml中设置,这个主要是在AndroidManifest.xml 中查找activity,然后在里面设置属性,如下 <application android:label="@str ...
- 73.fseek与宽字符读取文件
fseek //文件路径 ] = "1.txt"; //FILE *pf = fopen(path, "a+");//尾部添加,文件指针在尾部 //FILE * ...