[Angular] $q.all()
all(promises);
Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.
defer: 推迟做某事
defer.promise.then(successCallback, errorCallback, notifyCallback):许诺要做的事
resolve:决定现在做某事,
Awaiting multiple promises running in parallel
Assume having to call different backend services to collect data. The application can only continue once all the data has been successfully collected (success) or if at least one of the server call fails (failure). For maximum performance all tasks shall run in parallel. To await multiple tasks running in parallel the $q service offers us the all function. The all function in turn returns a promise. The promise is fulfilled if all parallel running tasks are fulfilled or the promise fails if at least one of the tasks fails. If we have three asynchronous tasks the code looks like this.
Read More:
http://lostechies.com/gabrielschenker/2014/02/04/angularjspart-11-promises/
var app = angular.module("app", []); app.controller("AppCtrl", function ($q, $timeout) { var one = $q.defer(); var two = $q.defer(); var three = $q.defer(); function success(data){ console.log(data); }
/* Not necessary to be here if use $q.all([promises array]).then(success) one.promise.then(success); two.promise.then(success); three.promise.then(success);
*/ var all = $q.all([one.promise, two.promise, three.promise]); all.then(success); $timeout(function () { one.resolve("one is done"); }, Math.random() * 1000); $timeout(function () { two.resolve("two is done"); }, Math.random() * 1000); $timeout(function () { three.resolve("three is done"); }, Math.random() * 1000); })
相关文章
- 【Angular专题】 (3)装饰器decorator,一块语法糖
- [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
- [Angular] Configurable Angular Components - Content Projection and Input Templates
- [Angular] NgRx/effect, why to use it?
- [Angular 2] Generate and Render Angular 2 Template Elements in a Component
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
- [Angular] Upgrade existing Angular app to Progressive Web App
- [Angular2 Form] Create Radio Buttons for Angular 2 Forms
- [Angular2 Router] Lazy Load Angular 2 Modules with the Router
- [Angular 2] ROUTING IN ANGULAR 2 REVISITED
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
- SAP UI5和Angular里控制器(Controller)实现逻辑比较
- Angular SSR 应用启动时的一些保护措施
- 关于 Angular view Query 的 id 选择器问题的单步调试
- Angular 项目里 angular.json 文件内容的学习笔记
- Angular单元测试里使用fixture.debugElement测试UI界面元素
- Angular view container删除view实例的过程
- Angular里的structural directive的一个例子
- Angular前端事件处理函数的形参名必须为某个硬编码值的怪事