angularJS $http $q $promise

 

一天早晨,爹对儿子说:“宝儿,出去看看天气如何!”

每个星期天的早晨,爹都叫小宝拿着超级望远镜去家附近最高的山头上看看天气走势如何,小宝说没问题,我们可以认为小宝在离开家的时候给了他爹一个promise。

这时候,他爹就想了,如果明天艳阳高照,他就准备去钓鱼,如果天实在不行,就作罢,如果小宝对预报明天的天气也没底,他就在家宅一天哪也不去。

大概过了半小时,小宝回来了。每周的结果不尽相同:

A计划 :天气晴朗

小宝不辱使命,说外面阳光明媚,万里无云,这个promise was resolved(小宝信守承诺),爹就可以收拾行装,钓鱼去鸟。

B计划: 小宝日观天象,阴转小雨的节奏

小宝依然不辱使命,但是天公不作美,promise was resolved,但是孩儿他爹觉得还是搁家呆着吧。

C计划:天象诡谲,小宝无法做出天气走势判断

小宝败兴而归,云雾重重,遮蔽了视线,不敢妄言天气走势,小宝走的时候立下承诺说要给他爹预报天气,但是没有成功,我们说promise was rejected!孩儿他爹决定小心驶得万年船,还是在家吧。

上述种种,用代码写出来是什么样子呢?

我们可以把孩儿他爹看成controller,小宝就是service。

整理逻辑:孩儿他爹让小宝去看天气,小宝不能立刻告诉他,但是孩儿他爹在等待结果的这段时间里可以抽抽烟,喝喝茶啥的,因为小宝承诺会把天气情况搞清楚。等小宝把天气预报带回来,他就可以决定下一步干啥。各位看官注意了:小宝登高望远看天气的时候并没有影响他爹干别的事情,这就是promise的妙处所在。

Angular里有个then()函数,我们可以决定孩儿他爹到底是用哪个计划(A,B,C),then()接收两个functions作为参数,第一个在promise is resolved的时候执行,另一个在promise is rejected的时候执行

Controller: FatherCtrl 

孩儿他爹掌控局面的代码如下:

// function somewhere in father-controller.js
var makePromiseWithSon = function () {

    // This service's function returns a promise, but we'll deal with that shortly
    SonService.getWeather()

    // then() called when son gets back
    .then(function (data) {

        // promise fulfilled
        if (data.forecast === 'good') {
            prepareFishingTrip();
        } else {
            prepareSundayRoastDinner();
        }
    }, function (error) {

        // promise rejected, could log the error with: console.log('error', error);
        prepareSundayRoastDinner();
    });
};

Service: SonService

小宝的作用就是充当了一个service,他爬上山头看天象,有点类似调用weather API,而且还是异步调用,他得到的结果可能是个变量,也有可能出现异常情况(比如,返回500—>大雾弥漫)。

从”Fishing Weather API”返回一个promise,如果it was resolved,就格式化成{“forecase”:”good”}。

app.factory('SonService', function ($http, $q) {
    return {
        getWeather : function () {

            // the $http API is based on the deferred/promise APIs exposed by the $q service

            // so it returns a promise for us by default
            return $http.get('http://fishing-weather-api.com/sunday/afternoon')
            .then(function (response) {
                if (typeof response.data === 'object') {
                    return response.data;
                } else {

                    // invalid response
                    return $q.reject(response.data);
                }

            }, function (response) {

                // something went wrong
                return $q.reject(response.data);
            });
        }
    };
});

总结

这个比喻向我们展示了异步的实质,孩儿他爹不会倚门等待儿子的归来,这段时间他完全可以自由活动。 孩儿他爹到底用哪个计划取决于(天气好/坏,没有成功预报),小宝在临走的时候给他爹一个promise,就等他回来的时候决定是resolve还是reject。

小宝其实是在使用异步服务(观天象—调用weather API)来获取天气信息,孩儿他爹根本就不懂技术,坐等结果即可!

 

=======================我是分割线====================

 

先说说什么是Promise,什么是$q吧。Promise是一种异步处理模式,有很多的实现方式,比如著名的Kris Kwal's Q还有JQuery的Deffered。

 

以前了解过Ajax的都能体会到回调的痛苦,同步的代码很容易调试,但是异步回调的代码,会让开发者陷入泥潭,无法跟踪,比如:

funA(arg1,arg2,function(){
    funcB(arg1,arg2,function(){
        funcC(arg1,arg2,function(){
             xxxx....
        })
    })   
})

本身嵌套就已经很不容易理解了,加上不知何时才触发回调,这就相当于雪上加霜了。

但是有了Promise这种规范,它能帮助开发者用同步的方式,编写异步的代码,比如在AngularJS中可以使用这种方式:

deferABC.resolve(xxx)
.then(funcSuccess(){},funcError(){},funcNotify(){});

当resolve内的对象成功执行,就会触发funcSuccess,如果失败就会触发funcError。有点类似

deferABC.resolve(function(){
    Sunccess:funcSuccess,
    error:funcError,
    notify:funcNotify
})

再说的直白点,Promise就是一种对执行结果不确定的一种预先定义,如果成功,就xxxx;如果失败,就xxxx,就像事先给出了一些承诺。

比如,小白在上学时很懒,平时总让舍友带饭,并且事先跟他说好了,如果有韭菜鸡蛋就买这个菜,否则就买西红柿炒鸡蛋;无论买到买不到都要记得带包烟。

 

小白让舍友带饭()
.then(韭菜鸡蛋,西红柿炒鸡蛋)
.finally(带包烟)

q服务是AngularJS中自己封装实现的一种Promise实现,相对与Kris Kwal's Q要轻量级的多。
先介绍一下$q常用的几个方法:

    • defer() 创建一个deferred对象,这个对象可以执行几个常用的方法,比如resolve,reject,notify等
    • all() 传入Promise的数组,批量执行,返回一个promise对象
    • when() 传入一个不确定的参数,如果符合Promise标准,就返回一个promise对象。

在Promise中,定义了三种状态:等待状态,完成状态,拒绝状态。

关于状态有几个规定:

  • 1 状态的变更是不可逆的
  • 2 等待状态可以变成完成或者拒绝

defer()方法

在$q中,可以使用resolve方法,变成完成状态;使用reject方法,变成拒绝状态。

下面看看 $q的简单使用:

<html ng-app="myApp">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body>
    <div ng-controller="myctrl">
        {{test}}
    </div>
    <script type="text/javascript">
         var myAppModule = angular.module("myApp",[]);
         myAppModule.controller("myctrl",["$scope","$q",function($scope, $ q ){
            $scope.test = 1;//这个只是用来测试angularjs是否正常的,没其他的作用

            var defer1 = $q.defer();
            var promise1 = defer1.promise;

            promise1
            .then(function(value){
                console.log("in promise1 ---- success");
                console.log(value);
            },function(value){
                console.log("in promise1 ---- error");
                console.log(value);
            },function(value){
                console.log("in promise1 ---- notify");
                console.log(value);
            })
            .catch(function(e){
                console.log("in promise1 ---- catch");
                console.log(e);
            })
            .finally(function(value){
                console.log('in promise1 ---- finally');
                console.log(value);
            });

            defer1.resolve("hello");
            // defer1.reject("sorry,reject");
         }]);
    </script>
</body>
</html>

其中defer()用于创建一个deferred对象,defer.promise用于返回一个promise对象,来定义then方法。then中有三个参数,分别是成功回调、失败回调、状态变更回调。

其中resolve中传入的变量或者函数返回结果,会当作第一个then方法的参数。then方法会返回一个promise对象,因此可以写成

xxxx
.then(a,b,c)
.then(a,b,c)
.then(a,b,c)
.catch()
.finally()

继续说说上面那段代码,then...catch...finally可以想想成java里面的try...catch...finally。

all()方法

这个all()方法,可以把多个primise的数组合并成一个。当所有的promise执行成功后,会执行后面的回调。回调中的参数,是每个promise执行的结果。
当批量的执行某些方法时,就可以使用这个方法。

var funcA = function () {
    console.log("funcA");
    return "hello,funA";
}
var funcB = function () {
    console.log("funcB");
    return "hello,funB";
}
$q.all([funcA(), funcB()])
.then(function (result) {
    console.log(result);
});

执行的结果:

funcA
funcB
Array [ "hello,funA", "hello,funB" ] 

when()方法

when方法中可以传入一个参数,这个参数可能是一个值,可能是一个符合promise标准的外部对象。

var funcA = function(){
    console.log("funcA");
    return "hello,funA";
}
$q.when(funcA())
.then(function(result){
    console.log(result);
});

当传入的参数不确定时,可以使用这个方法。

hello,funA

 

 

 

 

 

 

 

 

 

参考文章:

http://www.cnblogs.com/xing901022/p/4928147.html

http://www.cnblogs.com/sword-successful/p/4626797.html

http://blog.jobbole.com/51178/

 

http://www.cnblogs.com/52cik/p/promise.html

posted @ 2016-02-03 12:59  zcynine  阅读(585)  评论(0编辑  收藏  举报