带有`$q`的AngularJS执行顺序——链接承诺 [英] AngularJS execution order with `$q` -- Chaining Promises

查看:27
本文介绍了带有`$q`的AngularJS执行顺序——链接承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下方法有效:

$q.when().then(checkCookieToken)//检查 cookie 是否已经存在,例如在饼干.then(setHeader)//使用 REST-Token 设置 Header,例如来自 cookie.then(checkTokenOnline)//如果不OK 退出.then(getMenu)//如果前一个 OK 获取导航菜单.then(getDataResource)//设置 ngResource.then(getData);//并查询它

4 个问题:

1) 如果例如checkTokenOnline 不行,我不想执行剩下的功能,这时候怎么退出(exit,break,whatever,..)?

2) 如何将其中一些设置为并行,其中一些设置为串行?

3) 如何在它们之间传输数据?

4) 我怎样才能让下面的函数依赖于它之前的结果?

解决方案

您问的是如何在 Promise 中链接函数.

<块引用>

3) 如何在它们之间传输数据?

4) 如何让下面的函数依赖于它之前的结果?

返回中下一个函数的数据(或承诺):

var p2 = p1.then ( function (data) {var nextData = someFn(data);返回下一个数据;});var p3 = p2.then ( 函数 (nextData) {var nextData2 = someOtherFn(nextData);返回 nextData2;});//返回进一步链接返回 p3;

<块引用>

1) 如果例如checkTokenOnline 不行,我不想执行rest函数,这时候怎么退出(exit,break,whatever,..)?

拒绝一个承诺,让你的函数抛出一个错误.该链将跳过所有 .then 方法,直到您提供错误处理程序.

var p2 = p1.then ( function checkTokenOnline (response) {如果(isBadFn(响应){抛出错误;} 别的 {返回下一个数据;}}) .then ( someFn) .then ( someOtherFn) .catch ( 函数 (错误) {//someFn 和 someOtherFn 被跳过//记录错误抛出错误;});//返回进一步链接返回 p2;

<块引用>

2) 如何将其中一些设置为并行,其中一些设置为串行?

要使两个函数并行运行,请做出两个承诺.使用 $q.all 等待它们都完成.

var p1 = $q.when ( fn1() );var p2 = $q.when ( fn2() );var p3 = $q.all ( [p1, p2] );var p4 = p3.then ( 函数 (responseList) {var response1 = responseList[0];var response2 = responseList[1];返回一些东西;}).捕获(函数(错误){//记录错误抛出错误;});//返回进一步链接返回 p4;

请注意,$q.all 不是弹性.如果任何承诺抛出错误,.then 方法将被跳过,只有第一个错误会转到 .catch 方法.

函数式编程的经验法则是总是返回一些东西.

<小时>

有用的链接

Following Approach works:

$q.when()
        .then(checkCookieToken)         // check if cookie already exists e.g. in cookie
        .then(setHeader)                // set Header with REST-Token e.g from cookie
        .then(checkTokenOnline)         // if not OK logout
        .then(getMenu)                  // if previous OK get navigation menu
        .then(getDataResource)          // set ngResource
        .then(getData);                 // and query it

4 Questions:

1) If e.g. checkTokenOnline is not OK, I don't want to execute the rest functions, how can I quit (exit, break, whatever,..) at this point?

2) How can I set some of them parallel and some of them serial ?

3) How can I transfer data between them?

4) How can I make the following function dependend from its previous result?

解决方案

You are asking how to chain functions in promises.

3) How can I transfer data between them?

4) How can I make the following function depend on its previous result?

Return data (or a promise) for the next function in the chain:

var p2 = p1.then ( function (data) {
     var nextData = someFn(data);
     return nextData;
});

var p3 = p2.then ( function (nextData) {
     var nextData2 = someOtherFn(nextData);
     return nextData2;
});

//return for further chaining
return p3;

1) If e.g. checkTokenOnline is not OK, I don't want to execute the rest functions, how can I quit (exit, break, whatever,..) at this point?

To reject a promise, have your function throw an error. The chain will skip all the .then methods until you supply an error handler.

var p2 = p1.then ( function checkTokenOnline (response) {
             if ( isBadFn(response) {
                 throw error;
             } else {
                 return nextData;
             }
   }) .then ( someFn 
    ) .then ( someOtherFn
    ) .catch ( function (error) {
          // someFn and someOtherFn skipped
          //log error
          throw error;
   });

 //return for further chaining
 return p2;

2) How can I set some of them parallel and some of them serial ?

To make two functions run in parallel, make two promises. Use $q.all to wait for them both to complete.

var p1 = $q.when ( fn1() );
var p2 = $q.when ( fn2() );

var p3 = $q.all ( [p1, p2] );

var p4 = p3.then ( function (responseList) {
      var response1 = responseList[0];
      var response2 = responseList[1];
      return something;
}). catch ( function (error) {
      //log error
      throw error;
});

//return for further chaining
return p4;

Be aware that $q.all is not resilient. If any promise throws an error, the .then method will be skipped and only the first error will go to the .catch method.

The rule of thumb for functional programming is always return something.


Useful links

这篇关于带有`$q`的AngularJS执行顺序——链接承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆