JSFiddle - React, Tailwind, and code Playground

by cherishpeace

JavaScript

var Deffered = function(){
	this.promise = new Promise()
}


Deffered.prototype.resolve = function(obj){
	var handlelist = this.promise.queue;
	var handler = null;
	var returnVal = obj;
	while((handler = handlelist.shift()) != undefined){
		returnVal = handler.resolve.call(this,returnVal);
		if (returnVal.isPromise) {
			returnVal.queue = handlelist;
			//this.promise = returnVal;
			//this.promise.queue = handlelist;
			return;
		}
	}
}


Deffered.prototype.reject = function(){
	var handlelist = this.promise.queue;
	var handler = null;
	var returnVal = obj;
	while((handler = handlelist.shift()) != undefined){
		returnVal = handler.reject.call(this,returnVal);
		if (returnVal.isPromise) {
			returnVal.queue = handlelist;
			//this.promise = returnVal;
			//this.promise.queue = handlelist;
			return;
		}
	}
}


var Promise = function(){
	this.queue = [];
	this.isPromise = true;

}

Promise.prototype.then = function(onfulled,onrejected){
	var handler = {};
	if (onfulled) {
		handler['resolve'] = onfulled;
	}

	if (onrejected) {
		handler['reject'] = onrejected;
	}
	this.queue.push(handler);

	return this;
}


function offWork(callback){
    console.log("上班ing。。。")
    setTimeout(function(){
        console.log("下班了。。。")
        callback();
    },1000);
}
function backHome(callback){
    setTimeout(function(){
        console.log("到家了!!!")
        callback();
    },1000);
    console.log("回家ing。。。")
}


function start(){
    var d = new Deffered();
    
    offWork(function(){
        d.resolve('done----offWork');
    })    
    return d.promise;
        
}



start().then(function(){
    var d = new Deffered();
    backHome(function(){
        d.resolve('done----backhome');
    })   
    return d.promise;        
})