简体   繁体   English

角度承诺对象之间的差异

[英]Difference between angular promise objects

What is the difference between promise 1 and promise 2? 承诺1和承诺2有什么区别? The 1st one has only $$state property, the second one has additionally error and success callback. 第一个只有$$ state属性,第二个还具有错误和成功回调。 Why are they different? 他们为什么不同?

<div ng-app="app">
 <div ng-controller="TodoCtrl">

  </div>
 </div>




angular.module('app', [])
 .controller('TodoCtrl', function ($scope, $http) {
//promise 1    
console.log('p1',$http.get("/echo/json/").then(
  function(){ console.log('s1',arguments); }, 
  function(){ console.log('e1',arguments); }
 )
);

var p = $http.get("/echo/json/");
//promise 2
console.log('p2',p);
p.then(
  function(){ console.log('s2',arguments);}, 
  function(){console.log('e2',arguments);});
}
);

console log says: 控制台日志中说:

p1 Promise { $$state: Object }
   $$state: Object
   __proto__: Object
 p2 Promise { $$state: Object } 
      $$state: Object
      error: (fn) 
      success: (fn)
      __proto__: Object

In your first case, you call $http.get("/echo/json/") that returns a Promise. 在第一种情况下,您调用$ http.get(“ / echo / json /”)返回一个Promise。 After that you apply "then" method with callbacks to this promise (that returns you new promise), and after that your console output is executed (and you have this new promise there). 之后,将带有回调的“ then”方法应用于此promise(返回新的promise),然后执行控制台输出(然后在这里有了这个新promise)。 Callback functions are executed when real result comes back. 当实际结果返回时,将执行回调函数。 In the second case, you create promise, logs it (as a first created promise, nothing more, no callbacks), and after that you apply "then" method with callbacks to your promise. 在第二种情况下,您创建承诺,并将其记录下来(作为第一个创建的承诺,仅此而已,没有回调),然后将带有回调的“ then”方法应用于您的承诺。 Again, callback functions will be executed after results are received. 同样,将在收到结果后执行回调函数。 You can find description in Promise API documentation . 您可以在Promise API文档中找到说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM