LazyMan 제목

1947 단어
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")

좋은 웹페이지 즐겨찾기