gpt4 book ai didi

javascript - .then 的 AngularJS 条件案例基于仅在第一个 .then 之前已知的 var 值

转载 作者:行者123 更新时间:2023-11-29 23:44:06 26 4
gpt4 key购买 nike

我有一个工作正常的代码:

    myService.myFunc1()
.then(myService.myFunc1)
.then(function(dataA) {
// do something
})
.then(myService.myFunc2)
.then(function(dataB) {
//do something
});

但是,在执行 myFunc1 时, bool 值 myService.trig 已正确设置。我想更改上面的代码,使其成为基于 myService.trig bool 值的条件,并决定是否在 myService.myFunc1( ). 或彼此执行 .then INSTEAD(对于 false)。

如何用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
});

或者您可以嵌套 promise,这样您就不必一直检查值。但老实说,我更喜欢反复检查而不是嵌套 promise 。

关于javascript - .then 的 AngularJS 条件案例基于仅在第一个 .then 之前已知的 var 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44585373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com