角度 Q 怎么工作?

谁能解释一下在 AngularJS 中 $q.when是如何工作的? 我试图分析 $http是如何工作的,发现了这个:

var promise = $q.when(config);

下面是来自 Chrome 控制台的 config 对象:

Object {transformRequest: Array[1], transformResponse: Array[1], cache: Object, method: "GET", url: "/schedule/month_index.html"…}
cache: Object
headers: Object
method: "GET"
transformRequest: Array[1]
transformResponse: Array[1]
url: "/schedule/month_index.html"
__proto__: Object

接下来会发生什么? 这个对象如何被解析或拒绝?

73167 次浏览

Calling $q.when takes a promise or any other type, if it is not a promise then it will wrap it in a promise and call resolve. If you pass a value to it then it is never going to be rejected.

From the docs:

Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted.