AngularJS .then 的条件案例基于仅在第一个 .then 之前已知的 var 值
AngularJS conditional case for .then based on var value known only before the first .then
我有一个运行良好的代码:
myService.myFunc1()
.then(myService.myFunc1)
.then(function(dataA) {
// do something
})
.then(myService.myFunc2)
.then(function(dataB) {
//do something
});
然而,在 myFunc1
执行时,布尔值 myService.trig
被正确设置。我想更改上面的代码,使其基于 myService.trig
布尔值,并决定是在 myService.myFunc1().
之后执行所有其余的 .then (为真)还是执行另一个 .then INSTEAD (假)。
如何用angularJS方式完成?
谢谢。
我认为您必须在每个回调中检查``myService.trig` 的值是什么。
myService.myFunc1()
.then(() => {
// execute this function if the "myService.trig" is true
if (!myService.trig) return;
return myService.myFunc1;
})
.then((dataA) => {
// execute this function if the "myService.trig" is true
if (!myService.trig) return;
// do something
})
.then(() => {
// execute this function if the "myService.trig" is false
if (myService.trig) return;
return myService.myFunc2;
})
.then((dataB) => {
// execute this function if the "myService.trig" is false
if (myService.trig) return;
// do something
});
或者您可以嵌套承诺,这样您就不必一直检查值。但老实说,我更喜欢重复检查而不是嵌套承诺。
我有一个运行良好的代码:
myService.myFunc1()
.then(myService.myFunc1)
.then(function(dataA) {
// do something
})
.then(myService.myFunc2)
.then(function(dataB) {
//do something
});
然而,在 myFunc1
执行时,布尔值 myService.trig
被正确设置。我想更改上面的代码,使其基于 myService.trig
布尔值,并决定是在 myService.myFunc1().
之后执行所有其余的 .then (为真)还是执行另一个 .then INSTEAD (假)。
如何用angularJS方式完成?
谢谢。
我认为您必须在每个回调中检查``myService.trig` 的值是什么。
myService.myFunc1()
.then(() => {
// execute this function if the "myService.trig" is true
if (!myService.trig) return;
return myService.myFunc1;
})
.then((dataA) => {
// execute this function if the "myService.trig" is true
if (!myService.trig) return;
// do something
})
.then(() => {
// execute this function if the "myService.trig" is false
if (myService.trig) return;
return myService.myFunc2;
})
.then((dataB) => {
// execute this function if the "myService.trig" is false
if (myService.trig) return;
// do something
});
或者您可以嵌套承诺,这样您就不必一直检查值。但老实说,我更喜欢重复检查而不是嵌套承诺。