带有$ q的AngularJS执行顺序-链式承诺 [英] AngularJS execution order with `$q` -- Chaining Promises
问题描述
以下方法有效:
$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个问题:
1)如果checkTokenOnline
不好,我不想执行其余功能,如何在这一点上退出(退出,中断等等)?
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)如何设置其中一些并行和某些串行?
2) How can I set some of them parallel and some of them serial ?
3)如何在它们之间传输数据?
3) How can I transfer data between them?
4)如何使以下函数从其先前结果中得到依赖?
4) How can I make the following function dependend from its previous result?
推荐答案
您正在问如何在promise中链接功能.
You are asking how to chain functions in promises.
3)如何在它们之间传输数据?
3) How can I transfer data between them?
4)如何使以下功能取决于其先前的结果?
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)如果checkTokenOnline不好,我不想执行其余功能,如何在这一点上退出(退出,中断等等)?
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?
要拒绝一个诺言,请让您的函数抛出一个错误.链将跳过所有.then
方法,直到您提供错误处理程序.
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)如何设置其中一些并行和某些串行?
2) How can I set some of them parallel and some of them serial ?
要使两个功能并行运行,请做出两个承诺.使用$q.all
等待它们都完成.
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;
请注意,$q.all
不是弹性的.如果任何承诺引发错误,则将跳过.then
方法,并且只有第一个错误将进入.catch
方法.
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.
- AngularJS $q Reference - Chaining promises
- You're Missing the Point of Promises.
- Ninja Squad -- Traps, anti-patterns and tips about AngularJS promises
这篇关于带有$ q的AngularJS执行顺序-链式承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!