zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[AngularJS] Promises: Chain Promises - 1

angularjs Chain promises
2023-09-14 08:59:21 时间

Chaining promises

The return value of the then function of the promise is again a promise.(That maybe why in chain promise we can skip new a defer promise, just use $q.resolve()) This allows us to combine or chain asynchronous tasks. We can write

image

Now we need to define the test2 function in the controller which will be executed whenever the user clicks on the button.

image

 

It is important to note that the first on-success function defined on line 25 gets the value ‘Hurray’ injected as defined when resolving the promise. On line 28 we define the return value of the promise which will be injected to the second on-success function defined on line 33. The same is true for the on-failure functions. If we omit to define a return value as on line 28 then the value of the result parameter on line 33 would be undefined.

Without running the application try to determine what message the alert boxes on line 34 and 36 will show.