Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用重定向到承诺链 我使用承诺来调用一系列Web服务,但在某些情况下,我希望在逻辑的中间使用重定向。问题在于,在发生重定向后,承诺链将继续执行_Javascript_Angularjs_Promise - Fatal编程技术网

Javascript 使用重定向到承诺链 我使用承诺来调用一系列Web服务,但在某些情况下,我希望在逻辑的中间使用重定向。问题在于,在发生重定向后,承诺链将继续执行

Javascript 使用重定向到承诺链 我使用承诺来调用一系列Web服务,但在某些情况下,我希望在逻辑的中间使用重定向。问题在于,在发生重定向后,承诺链将继续执行,javascript,angularjs,promise,Javascript,Angularjs,Promise,代码如下: myfunc = function() { return PromiseCall1() .then(function(data){ if (condition){ $location.path(some_url); // This is a redirection with AngularJS } else { return PromiseCall2(); } }) .then(function(data){

代码如下:

myfunc = function() {
  return PromiseCall1()

  .then(function(data){
    if (condition){
      $location.path(some_url); // This is a redirection with AngularJS

    } else {
      return PromiseCall2();
    }
  })

  .then(function(data){
    return PromiseCall3();
  })

  .then(function(data){
    // Some other stuff here
  })

};
预期的行为将是:

  • 必须调用PromiseCall1()
  • 如果
    条件
    为true,则不必调用其他承诺,只需执行重定向即可
  • 如果
    条件
    为false,则必须调用Promise2,然后调用Promise3
你会怎么做?
谢谢您的帮助。

佩特卡的解决方案还可以。如果你愿意的话。如果你用Maybe包装你的处理器,这是可能的

func = function () {
    return PromiseCall1().then(function (data) {
        if (condition) {
            $location.path(some_url); // This is a redirection with AngularJS
        } else {
            return PromiseCall2().then(function (data) {
                return PromiseCall3();
            }).then(function (data) {
                // Some other stuff here
            });
        }
    });
};
比如:

var maybe = function(fn){
     return function(value){
          if(value === maybe.Nothing) return maybe.Nothing;
          return fn.apply(this,arguments);
     };
});
maybe.Nothing = {}; // unique reference
那你可以做什么

Promise.try(function(){
    return 15;
}).then(maybe(function(val){
    console.log("got",val);    
    return maybe.Nothing;
})).then(maybe(function(val){
    console.log("This never gets called");  
}));

这为您提供了一个显式的值钩子,表示在用maybe包装的链中不继续任何承诺。

我将在myFunc范围中创建另一个承诺对象。在每个链检查中,如果未解决,则继续执行,如果已解决,则从“then”处理程序返回。当你重定向时,你可以解决承诺,这将使所有的处理程序返回。是的,从我的发现来看,这种方法可能是你最好的选择。谢谢你的选择。我会看看什么时候我对承诺的理解会更好。