• 迷你版Deferred


    直接贴代码:

    /**
     * 迷你版的deferred
     */
    function Deferred(func) {
        if (this instanceof Deferred === false) {
            return new Deferred(func)
        }
        var tuple = [];
        var promise = {
            resolve: function () {
                var t;
                while (t = tuple.shift()) {
                    t.apply(null, arguments);
                }
            },
            then: function (n) {
                return tuple.push(n), this;
            }
        }
        if (func) {
            func.call(promise, promise.resolve);
        }
        return promise;
    }

    demo1:

    var d = new Deferred();
    
    setTimeout(function () {
        d.resolve({
            name : 'breezefeng',
            age : 24,
            sex : '男'
        });
    });
    
     d.then(function (globalInfo) {
        console.log(globalInfo.name);
    }).then(function (globalInfo) {
        console.log(globalInfo.age);
    }).then(function (globalInfo) {
        console.log(globalInfo.sex);
    });

    demo2:

    Deferred(function (resolve) {
        setTimeout(function () {
            resolve('aaaa');
        });
    }).then(function (bbb) {
        console.log(bbb)
    })
  • 相关阅读:
    Python3 模块
    python os 方法
    python第三方模块的导入
    深拷贝和浅拷贝的区别
    win10专业版激活方法
    Python3 JSON
    python函数
    去重 方法
    VUE-地区选择器(V-Distpicker)组件使用
    ajax
  • 原文地址:https://www.cnblogs.com/fengyuqing/p/deferred-mini.html
Copyright © 2020-2023  润新知