[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 ...
随机推荐
- 【Django】缓存
由于Django是动态网站,所以每次请求都会去数据库中进行响应的操作. 当程序访问量大时,耗时必然会更加明显,最简单的解决方案就是使用缓存. Django中的缓存: ==即将某一个view的返回值保存 ...
- 一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody
简介 JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请 ...
- HDU 3400 Line belt (三分再三分)
HDU 3400 Line belt (三分再三分) ACM 题目地址: pid=3400" target="_blank" style="color:rgb ...
- HDU 3487(Play with Chain-Splay)[template:Splay]
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 測试CPU支持指令集AVX,AVX2,SSE情况的代码【VS2010调试通过】
完整代码例如以下所看到的 http://download.csdn.net/detail/vbskj/7723827 本人的測试结果 watermark/2/text/aHR0cDovL2Jsb2cu ...
- 推断一个java文件和邮箱格式是否合法
import java.util.Scanner; public class StringTest { public static void main(String[] args) { int bac ...
- php实现希尔排序(总结)
php实现希尔排序(总结) 一.总结 1.希尔排序的算法思路:分组排序, 缩小增量排序,插入排序 2.算法思路: 循环非常好写 有几次gap:log2(n) 每次gap有几组:gap组 每组有几个元素 ...
- 41.C++多线程生产消费者模型
#include <iostream> #include <thread> #include <mutex> #include <condition_vari ...
- Mac 终端操作数据库
名词解释: 事务:一个事务(transaction)中的所有操作,要么全部完成,要么全部不完成,不会结束在中间某个环节.事务在执行过程中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这 ...
- 配置spotlight连接linux服务器
本文转自(https://blog.csdn.net/qq_31391261/article/details/79429098) 一.配置spotlight连接linux服务器 1.以管理员身份运行软 ...