美文网首页
LazyMan题目

LazyMan题目

作者: sunny519111 | 来源:发表于2018-02-09 09:54 被阅读49次
    function _LazyMan(name) {
     
        this.promiseGetters = [];
     
        var makePromise = function  () {
            var promiseObj = new Promise(function(resolve, reject){
                console.log("Hi! This is " + name + "!");
     
                resolve();
            })
     
            return promiseObj;
        }
     
        this.promiseGetters.push(makePromise);
     
        // 在各个Promise的then函数中,将任务序列穿起来
        var self = this;
        var sequence = Promise.resolve();
        // Promise.resolve 等价于
        // var sequence = new Promise(function (resolve, reject) {
        //     resolve();
        // })
        setTimeout(function(){
            for (var i = 0; i < self.promiseGetters.length; i++) {
                var nowPromiseGetter = self.promiseGetters[i];
                var thenFunc = (function (nowPromiseGetter) {
                    return function  () {
                        return nowPromiseGetter()
                    }
                })(nowPromiseGetter);
     
                sequence = sequence.then(thenFunc);
            };
     
        }, 0); // 在下一个事件循环启动任务
    }
     
    _LazyMan.prototype.eat = function(name) {
        var makePromise = function  () {
            var promiseObj = new Promise(function(resolve, reject){
                console.log("Eat " + name + "~");
     
                resolve();
            })
     
            return promiseObj;
        }
     
        this.promiseGetters.push(makePromise);
     
        return this; // 实现链式调用
    }
     
    _LazyMan.prototype.sleep = function(time) {
        var makePromise = function  () {
            var promiseObj = new Promise(function(resolve, reject){
     
                setTimeout(function(){
     
                    console.log("Wake up after " + time + "s!");
     
                    resolve();
     
                }, time * 1000);
            })
     
            return promiseObj;
        }
     
        this.promiseGetters.push(makePromise);
     
        return this;
    }
     
    /* 封装 */
     
    function LazyMan(name){
     
        return new _LazyMan(name);
     
    }
    //  LazyMan("Hank")
    // LazyMan("Hank").sleep(1).eat("dinner")
    
    

    相关文章

      网友评论

          本文标题:LazyMan题目

          本文链接:https://www.haomeiwen.com/subject/dmhftftx.html