AngularJs $q 使用总结

时间:15-12-31 栏目:技术 作者:liva 评论:0 点击: 5,834 次

介绍

$q主要的用途就是用看似同步的代码实现异步的功能,$q可以帮助你解决更种回调,且看起来逻辑很清晰.研究了好久,终于明白基本用法.

API介绍

$q接口:

var ObjectDefferred = $q.defer() 返回一个deferred对象,一般情况下都基于deferred对象进行操作

$q.reject(...)    会创建一个promiss并且被解析成reject,用于链式操作的情景中

$q.when(value, [successCallback], [errorCallback], [progressCallback])     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. 这里包裹一个变量value,这个变量可以是promise可以是常量,任何类型都可以:如果是常量(非promiss)变量,相当于直接执行resolve操作,并且把这个变量传入success call back中; 如果是promise, 会调用相应的call back.

$q.resolve(value, [successCallback], [errorCallback], [progressCallback]); 返回一个resolve的promiss,一般用于链式操作.

$q.all(promises); 这里的promises是array类型,所有的promiss满足之后才会执行后续的操作(then),一般用于链式操作中.

deferred对象接口:

deferred对象的生成方式是调用 $q.defer()接口.deferred对象有如下的api:

resolve(value)     成功执行该任务,并且返回一个value对象

reject(reason)     失败执行该任务,并且返回一个value对象

notify(value)     通知该任务的执行进度,并且将相应的value对象返回

属性:

promise   建立一个promise关联这个deferred对象

promiss对象接口:

promise对象是deferred对象的一个属性,直接调用ObjectDefferred.promise就可以获取到

  • then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected,then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.This method returns a new promise which is resolved or rejected via the return value of the successCallbackerrorCallback(unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of the notifyCallback method. The promise cannot be resolved or rejected from the notifyCallback method.
  • catch(errorCallback) – shorthand for promise.then(null, errorCallback)
  • finally(callback, notifyCallback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.

相关例子:

链式调用完成后控制台打印出 1,2,3

链式调用完成后控制台打印出 1,15,可以看出,第一个return $q.reject(15)之后,直到最后一个then()才有错误回调函数,所以异常一直传递到最后,中间的几个then()没有错误回调函数。

控制台打印出I Love you!; 前面已经说过了when里面value如果是常量,会直接返回success callback

控制台打印出I Hate you!; $q.reject会生成一个reject的promise

 

控制台打印出I Love you!,Love story!,"Let't get wet!

在线演示的一个例子: http://jsfiddle.net/jeremylikness/Q2jMG/

总结:

$q无非就是一个采用then方式异步回调,关键在于对promise和defer()对象的理解

声明: 本文由( liva )原创编译,转载请保留链接: AngularJs $q 使用总结

AngularJs $q 使用总结:等您坐沙发呢!

发表评论


购物推荐

赞助商

© 2013 enjoydiy.com. Design by zijiao. 63 queries in 0.301 seconds, using 21.85MB memory