使用重定向到诺言链 [英] Using redirections into promises chain

查看:102
本文介绍了使用重定向到诺言链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的承诺来调用Web服务的意甲,但我想,在某些情况下,在我的逻辑的中间使用重定向。的问题是,许链增长继续执行重定向发生后

下面是code:

  MYFUNC =功能(){
  返回PromiseCall1()  。然后(功能(数据){
    如果(条件){
      $ location.path(SOME_URL); //这是与AngularJS重定向    }其他{
      返回PromiseCall2();
    }
  })  。然后(功能(数据){
    返回PromiseCall3();
  })  。然后(功能(数据){
    //此处是一些其他的东西
  })};

预期的行为将是:


  • PromiseCall1()必须名为

  • 如果条件是真实的,没有任何其他的承诺必须被调用,只是做重定向

  • 如果条件为假,那么Promise2必须Promise3调用。

你会怎么做呢?
谢谢你的帮助。


解决方案

  FUNC =功能(){
    返回PromiseCall1()。然后(功能(数据){
        如果(条件){
            $ location.path(SOME_URL); //这是与AngularJS重定向
        }其他{
            返回PromiseCall2()。然后(功能(数据){
                返回PromiseCall3();
            })。然后(功能(数据){
                //此处是一些其他的东西
            });
        }
    });
};

I'm using promises to call a serie of webservices, but I'd like, in some cases, use redirection in the middle of my logic. The problem is that the promises chain continue to execute after the redirection occured.

Here is the code:

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
  })

};

The expected behaviour would be:

  • PromiseCall1() must be called
  • if condition is true, no other promise must be called, just do the redirection
  • if condition is false, Promise2 then Promise3 must be called.

How would you do it? Thank you for your help.

解决方案

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
            });
        }
    });
};

这篇关于使用重定向到诺言链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆