[AngularJS] Promises: Chain Promises - 2. Chaining multiple asynchronous (server-) calls
Chaining multiple asynchronous (server-) calls
Imagine having a user id in you application. Now you want to load the details of the staff associated with the user account. One way of doing this is to first load the user details which might contain the id of the associated staff. Once we have this id we can then load the details of this user. In this situation we have to chain two asynchronous tasks. Using callback function this would be a quite complicated undertaken resulting in ugly and difficult to understand code, specifically if we also have to implement a robust exception handling. Using promises on the other hand makes the implementation really simple.
and this is the definition of the loadData function triggered by the button click. We define it in our TestCtrl controller.
Here we can see loadUser(), this function MUST return a promise.
The return value of this call contains amongst other values the id of the associated staff. Once we have that staff id we load the staff details (loadStaff on line 65). Finally we display the staff details as string in an alert box (line 67). In a real application we would probably use the Angular $http service or the $resourceservice to make a server call and get the data.
and the loadStaff like this
Read More: http://lostechies.com/gabrielschenker/2014/02/04/angularjspart-11-promises/
相关文章
- SQL SERVER 2000数据库置疑 紧急模式
- ORA-26837: Server “string” is not an XStream inbound server ORACLE 报错 故障修复 远程处理
- ORA-29327: unsupported client compatibility mode used when talking to the server ORACLE 报错 故障修复 远程处理
- SQL Server并行操作优化避免并行操作被抑制而影响SQL的执行效率
- MySQL Error number: MY-013141; Symbol: ER_SERVER_TABLE_CHECK_FAILED; SQLSTATE: HY000 报错 故障修复 远程处理
- sql server比较Oracle 与 SQL Server的优缺点(oracleltgt)
- SQL Server 实现代码换行的技巧(sqlserver换行)
- SQL Server繁体版:进步带来自由(sqlserver繁体版)
- 拟利用SQL Server进行数据库模拟的福音(sqlserver 模)
- 深入研究SQL Server日志表(sqlserver日志表)
- SQL Server加x锁实现数据的安全共享(sqlserver加x锁)
- SQL Server内连接实现数据查询的简洁之道(sqlserver内连接)
- 攀登青云之路:学习SQL Server(青云sqlserver)
- MySQL 中 Server 的重要性(mysql中server)