本文介绍了Chain Angular $http 调用正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于 $q 和 promises 好几天了,我似乎理解它......有点.我在实践中遇到过以下情况:

I have been reading about $q and promises for days now and I seem to understand it...somewhat. I have the following situation in practice:

  1. 发出 $http 请求并检查是否可以进行后续调用.
  2. 如果第一次调用失败,返回无数据",如果成功并说可以进行调用,则进行第二次调用,如果没有 - 再次无数据".如果第二次调用成功,则返回数据,否则返回无数据".它看起来像这样(大约,我简化了一般想法,所以不要担心这里的小错误):

  1. An $http request is made and checks whether a subsequent call can be made.
  2. If the first call fails, return "no data", if it succeeds and says a call can be made, the second call is made, if not - "no data" again. If the second call succeeds, it returns data, if not - "no data". It looks like this (approximately, I simplified for general idea, so don't worry about the minor mistakes here):

        return $http.get (something)
            .then(function(allowedAccess){
                if(allowedAccess){
                    return $http.get (somethingElse)
                        .then( function(result){return {data:result} },
                        function(error){return {data:"n0pe"} }
                    )
                } else {
                    return {data:"n0pe"}
                }
            },
            function(){ return {data:"n0pe"} });

有人告诉我在这里使用 $q.我真的不明白我如何或为什么会这样做.$http 调用已经是承诺.

I was told to use $q here. I don't really understand how or why I would. The $http calls are promises already.

如果有办法让这个更干净,我看不到.刚刚读完这篇关于这个主题的帖子.本质上,我是否遗漏了什么/是否有更好的方法来做到这一点?

If there is a way to make this cleaner, I don't see it. Just got done re-reading this post on the subject. Essentially, am I missing something / is there a better way to do this?

也只需重新阅读关于链接承诺的教程 - 它根本不处理呼叫失败.基本上将此作为尽职调查发布.

Also just re-read a tutorial on chaining promises - it doesn't handle call failures at all. Basically posting this as due diligence.

编辑 2:这更详细地阐述了我所询问的理论,摘自第一篇文章:

Edit 2: This is more of an elaborate on the theory I am asking about, excerpt from the first article:

不过这是一个简单的例子.如果您的 then() 回调返回另一个承诺,它将变得非常强大.在这种情况下,下一个 then() 只会在承诺解决后才会执行.例如,此模式可用于串行 HTTP 请求(其中请求取决于前一个请求的结果):

这似乎是在谈论这样的链:

This seems to be talking about chains like this:

   asyncFn1(1)
    .then(function(data){return asyncFn2(data)})
    .then(function(data){return asyncFn3(data)})

所以,如果我理解正确的话 a).不适用于我,因为我没有第三个功能.b).如果我有三个函数,将适用于我,因为当我在第一个 $http 请求中运行 if 语句时,只有在 if 语句中我才返回另一个承诺.所以,理论上,如果我有三个异步函数要链接,我是否需要将 if 语句放在一个 promise 中?

So, if I understand correctly this a). Doesn't apply to me because I don't have a 3rd function. b). Would apply to me if I had three functions because while I run an if statement inside the first $http request, and only inside the if statement do I return another promise. So, theoretically, if I had three async functions to chain, I would need to put my if statement inside a promise?

推荐答案

Promise 确实有助于进行异步调用的代码组合.换句话说,它们允许您以与编写同步调用集的方式类似的方式编写代码(使用链式 .thens),并且就好像同步代码是在 try/catch 块中(使用 .catch).

Promises really help with code composition of making async calls. In other words, they allow you to compose your code in a similar manner to how you would compose a synchronous set of calls (with the use of chained .thens) and as if it the sync code was in a try/catch block (with .catch).

那么,假设您的 HTTP 调用被阻塞了 - 您的逻辑看起来像这样:

So, imagine that your HTTP calls were blocking - the logic you have would look like so:

var allowedAccess, data;
try {
  allowedAccess = $http.get(something);

  if (allowedAccess){
    try{
      var result = $http.get(somethingElse);
      data = {data: result};
    } catch (){
      data = {data: "n0pe"};
    }
  } else {
    data = {data: "n0pe"};
  }
} catch (){
  data = {data: "n0pe"};
}
return data;

你可以稍微简化一下:

var allowedAccess, result;
try {
  allowedAccess = $http.get(something);
  var result;
  if (allowedAccess) {
     result = $http.get(somethingElse);
  } else {
     throw;
  }
  data = {data: result};
} catch () {
   data = {data: "n0pe"};
}
return data;

这将转化为异步版本:

return $http
          .get(something)
          .then(function(allowedAccess){
             if (allowedAccess){
               return $http.get(somethingElse);
             } else {
               return $q.reject(); // this is the "throw;" from above
             }
          })
          .then(function(result){
             return {data: result};
          })
          .catch(function(){
             return {data: "n0pe"};
          })

至少,这是您在编写具有分支和异步调用的代码时可以应用的推理.

At least, this is the reasoning you could apply when composing code with branches and async calls.

我并不是说我提供的版本是最佳的或更短的 - 它,但是,由于单个错误处理而更加枯燥.但是只要意识到当你做 .then(success, error) 时,它相当于 try/catch 在前面的异步操作上 - 这可能或者可能不需要,具体取决于您的具体情况.

I'm not saying that the version I presented is optimal or shorter - it is, however, more DRY because of a single error handling. But just realize that when you do .then(success, error) it is equivalent to try/catch over the previous async operation - this may or may not be needed depending on your specific circumstance.

这篇关于Chain Angular $http 调用正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:25