角执行顺序用`$ q` [英] Angular execution order with `$q`
问题描述
下面的方法如下:
$ q.when()
。然后(checkCookieToken)//检查是否已经cookie的存在,例如在饼干
。然后(的setHeader)//设置页眉与REST令牌从例如饼干
。然后(checkTokenOnline)//如果没有确定注销
。然后(使用getMenu)//如果previous确定的GET导航菜单
。然后(getDataResource)//设置ngResource
。然后(的getData); //和查询它
4个问题:
1)如果例如 checkTokenOnline
也不行,我不想执行,其余的功能,我怎么能在这一点退出(退出,休息,什么的,..)?
2)我如何设置他们中的一些平行,其中一些串行?
3)我如何在两者之间传输数据?
4)如何使以下功能dependend从previous结果?
正在询问如何链功能的承诺。
3)我如何在它们之间传递数据?
4)如何使以下功能依赖于它的previous结果?
块引用>返回在链中的下一个功能数据(或承诺)
VAR P2 = p1.then(功能(数据){
变种nextData = someFn(数据);
返回nextData;
});VAR P3 = p2.then(功能(nextData){
VAR nextData2 = someOtherFn(nextData);
返回nextData2;
});//返回进一步链接
返回P3;
1)如果例如checkTokenOnline也不行,我不想执行,其余的功能,我怎么能离开(退出,休息,什么的,..)在这一点?
块引用>要拒绝的承诺,有你的函数的扔错误。直到你提供一个错误处理程序链将跳过所有
。然后
方法。VAR P2 = p1.then(功能checkTokenOnline(响应){
如果(isBadFn(响应){
抛出的错误;
}其他{
返回nextData;
}
})。然后(someFn
)。然后(someOtherFn
).catch(功能(错误){
// someFn和someOtherFn跳过
//记录错误
抛出的错误;
}); //返回进一步链接
返回P2;
2)我如何设置他们中的一些平行,其中一些串行?
块引用>若要并行运行两个功能,提出两个承诺。使用
$ q.all
等待他们两个来完成。VAR P1 = $ q.when(FN1());
变种P2 = $ q.when(fn2的());VAR P3 = $ q.all([P1,P2]);VAR P4 = p3.then(功能(responseList){
变种response1 = responseList [0];
变种响应2 = responseList [1];
返回的东西;
})。赶上(功能(错误){
//记录错误
抛出的错误;
});//返回进一步链接
返回P4;请注意,
$ q.all
不是弹性。如果有任何承诺抛出一个错误,在。然后
方法将被跳过,只有第一个错误会去.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 link
Ninja Squad -- Traps, anti-patterns and tips about AngularJS promises
这篇关于角执行顺序用`$ q`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!