返回

直观的链式回调 MyPromise

 

function MyPromise(fun) {
	let self = this;
	self.then = function (resolve, error) {
		resolve&&resolve(self.value);
		error&&error(self.errValue);
		return this;
	}

	function resolve (value) {
		self.value = value;
	}

	function error (value) {
		self.errValue = value;
	}

	fun(resolve, error);
}

function test () {
	return new MyPromise(function(resolve,error){
		resolve("我是个正确值");
		error('我是个错误值');
	});
}

test().then(function(result){
	console.info('result = ', result)
}).then(function(result){
	console.info('result = ', result)
});

 

posted @ 2018-12-17 14:54  feige_hunter  阅读(123)  评论(0编辑  收藏  举报