关于promise

promise概念:

Promise对象用于表示一个异步操作的最终完成(或失败)及其结果值。

promise题目:

const first = ()=>(new Promise((resovlve,reject)=>{
	console.log(3);
	let p = new Promise((resolve,reject)=>{
		console.log(7);
 			setTimeout(()=>{
				console.log(5);
				resolve(6);
		 },0);
		resolve(1);
		});
	resolve(2);
	p.then(arg =>{
		console.log(arg);
	});
});

first().then(arg => {
	console.log(arg);
});
console.log(4);

//1.同步的代码 3、7、4
//2.微任务的异步代码(次高,then)1、2
//3.宏任务的异步代码(最低)5
//6不执行
//结果:374125



promise 实践:

【问题分析】
如何把大象放到冰箱

【答题思路】
开门/装大象/关门 都是异步的操作。
通过本题加强对promise的理解,理解promise解决的核心问题时什么。

console.time()

const openDoor=cb=>{
	setTimeout(cb,1000)
}
const putin=cb=>{
	setTimeout(cb,3*1000)
}
const closeDoor=cb=>{
	setTimeout(cb,1000)
}
const done = () =>{
	console.timeEnd();
	console.log("done");
}


openDoor(()=>{
	putIn(()=>{
		closeDoor(()=>{
			done();
		})
	})
})



//用promise解决
const openDoor=()=>	new Promise(res=>{
	setTimeout(res,1000)
	)}
const putin=()=>	new Promise(res=>{
	setTimeout(res,3*1000)
	)}
const closeDoor=()=>	new Promise(res=>{
	setTimeout(res,1000)
	)}
const done = () =>new Promise(res=>{
		console.timeEnd();
		console.log('done2');
		res();
	})
openDoor().then(putin).then(closeDoor).then(done)

如何实现链式调用:

【问题分析】
类似b.then().then().then()的链式调用是如何实现的?

【答题思路】
因为要不断的调用,所以一定是返回自己,或者返回一个和自己类似的结构。后续实现Promise的时候,会用得上!

class Test1{

	then(){
		console.log(6666);
		return this;
		}
}
var a = new Test1();
a.then().then().then()

class Test2{
	then(){
		console.log(77777);
		return new Test2();
	}
}

var b = new Test2();
b.then().then().then()

promise简单实现

简单实现resolve方法和then方法

const State = {
		pending:"pending",
		resolve:"resolve",
		reject:"reject"
	}
	class MyPromise{
		_state = State.pending;
		_value;
		_resArray = [];
		constructor(exclutor){
			exclutor(this._resolve.bian(this),this._reject.bind(this))
		}
		_resolve(val){
			this._value = val;
			this._state = State.resolve;
			
			this._resArray.forEach(item=>{
				item(this._value);
			})
			
		}
		_reject(){}
		then(onRes,onRej = ()=>{}){
			this._resArray.push(onRes);
		}
	}

实现then的链式调用

const State = {
	pending: "pending",
	resolved: "resolved",
	rejected: "rejected"
};

noop= ()=>{};

class MyPromise{

	constructor(exclutor){

		exclutor(this._resolve.bind(this),this._reject.bind(this));
	}
 _state = State.pending;
 _value;
 _resArray =[];
	_resolve(val){
	if(this._state === State.pending){
		this._value = val;
		this._state = State.resolved;
		// 执行then 传入的onRes
		this._runResArray();
		}
	}
	
	_reject() {}
	
	_runResArray(){
	this._resArray.forEach(item=>{
		const result =	item.handle(this._value); //拿到上一个promise的返回值
		const nextPromise = item.promise;
		if(result instanceof MyPromise){
			result.then(val => item.promise._resolve(val));
		}else{
			item.promise._resolve(result);
		}
		})
	}

	then(onRes,onRej = noop) {
	
		const newPromise = new MyPromise(()=>{});
		const item = { promise: newPromise, handle: onRes};
		this._resArray.push(item);
		
		

	if(this._state === State.resolved){
		this._runResArray();
	  }
	
	return newPromise;
	
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值