javascript ES6

generator构造器

function *show(){

yield 12;

yield 5;

}

var gen = show(); //show()是构造器,不是函数。所有show()不是执行函数,而是创建generator对象。 gen.next()开始执行。

console.log(gen.next());   //{value: 12, done: false}

console.log(gen.next());   //{value: 5, done: false}

console.log(gen.next());   //{value: undefined, done: true}

 

promise

promise作用:优化回调函数,用来传递异步操作的数据(消息)

异步:多个操作可以同时进行。

/**
 *Promise对象
 */
var p1 = new Promise(function (resolve, reject) {
        //resolve  成功时的函数
        //reject   失败时的函数
    });

var p1 = new Promise(function (resolve, reject) {
        if (异步处理成功了) { //假如异步处理成功
            resolve(异步处理完后->成功数据); //把异步数据传出去处理(then函数处理)
        } else {
            reject(异步处理完后->失败数据); //把异步数据传出去处理(then函数处理)
        }
    });

/**异步处理完再执行
 *@param resolve函数,reject函数
 *@return 返回promise对象,链式操作。
 */
p1.then(function () { //成功时被调用},function(){//失败时被调用});


    /**
     *promise实例,也是nodejs实例。
     */
    const fs = require('fs');
    fs.readFile('let.html', function (err, data) {
        var p1 = new Promise(function (resolve, reject) {
                if (err) {
                    reject(err);
                } else {
                    resolve(err);
                }
            });

        p1.then(function (value) {
            console.log(value.toString());
        }, function (value) {
            console.log(value);
        });
    });

    /**
     *promise实例
     */
    function ajax(url, fnSuccess, fnFail) {
        var oAjax = new XMLHttpRequest();
        oAjax.open('GET', url, true);
        oAjax.send();
        oAjax.onload = function () {
            if (oAjax.readState == 4 && oAjax.status == 200) {
                fnSuccess(oAjax.responseText);
            } else {
                fnFail(oAjax.status);
            }
        }
    }

    window.onload = function () {
        let oBtn = document.getElementById('btn1');
        let oBox = document.getElementById('box1');
        oBtn.onclick = function () {
            var p1 = new Promise(function (resolve, reject) {
                    ajax('a.txt', function (str) {
                        resolve(str);
                    }, function () {
                        reject(str);
                    });
                });

            p1.then(function () {
                oBox.innerHTML = str;
            }, function () {
                oBox.innerHTML = str;
            });
        }
    }

    /**
     *捕获异常
     * new Promise().catch()
     */
    var p1 = new Promise(function (resolve, reject) {
            resolve('成功了');
        });

    p1.then(function () {
        console.log(value);
        throw '发生错误了';
    }).catch (function (e) {
        console.log(e);
    });

    /**
     *用于将多个Promise对象组合包装成一个Promise对象。相当于事务概念
     * Promise().all()
     */
    var p1 = Promise.resolve(3);
    var p2 = Promise.reject(5);

    Promise.all([true, p1, p2]).then(function () {
        console.log(value);
    }, function () {
        console.log('错误了,' + value);
    });

    /**
     * 那个Promise先执行完返回那个
     * Promise().race()
     */
    var p1 = new Promise(function (resolve, reject) {
            setTimeout(resolve, 500, 'one');
        });

    var p1 = new Promise(function (resolve, reject) {
            setTimeout(resolve, 100, 'two');
        });

    Promise.race([p1, p2]).then(function (value) {
        console.log(value);
    });

 promise实例

 function p() {
     function p1() {
         return new Promise(function (resolve, reject) {
             setTimeout(function () {
                 console.log("aaaaa");
                 resolve('传递给then函数参数'); //只有调用resolve后面调用then才起效
             }, 3000);
         });
     }

     function p2() {
         return new Promise(function (resolve, reject) {
             setTimeout(function () {
                 console.log("bbbbb");
             }, 2000);
         });
     }

     p1().then(p2);
 }

 

posted @ 2017-04-02 18:50  N神3  阅读(345)  评论(0编辑  收藏  举报