functiondoSth(){let data =40returnnewPromise(function(resolve){resolve(data)})}functionPromise(executor){let state ='pending'let value
functionresolve(newVal){
state ='resolved'
value = newVal
}functionreject(err){}functionhandle(onResolved){onResolved(value)}executor(resolve, reject)this.then=function(onResolved){handle(onResolved)}}doSth().then((data)=>{
console.log(data)})
链式调用,需要.then返回promise
handle方法接收对象,保存resolve和cb
实例化promise,value第一次更新
每一次调用.then,.then的cb的返回值再次更新value
.then时不传cb,不会对value有动作,继续将这个value resolve下去
functiondoSth(){let data =10returnnewPromise((resolve, reject)=>{resolve(data)})}functionPromise(executor){let callback,
value
let state ='pending'functionresolve(data){
value = data
state ='resolved'}functionreject(){}functionhandle(handler){if(!handler.cb){
handler.resolve(value)return}let res = handler.cb(value)
handler.resolve(res)}executor(resolve)this.then=(cb)=>{returnnewPromise(function(resolve){handle({
resolve,
cb
})})}}doSth().then((data)=>{
console.log('p1', data)return20}).then().then((data)=>{
console.log('p2', data)})
某次.then调用中,返回了promise
functiondoSth(){let data =10returnnewPromise((resolve, reject)=>{resolve(data)})}functiondoSth2(data){
console.log('doSth2', data)// 20returnnewPromise((resolve)=>{resolve(data)})}functionPromise(executor){let callback,
value
let state ='pending'functionresolve(data){
value = data
state ='resolved'}functionreject(){}functionhandle(handler){if(!handler.cb){
handler.resolve(value)return}let res = handler.cb(value)
handler.resolve(res)}executor(resolve)this.then=(cb)=>{returnnewPromise(function(resolve){handle({
resolve,
cb
})})}}doSth().then((data)=>{
console.log('p1', data)// p1 10return20}).then((data)=>{returndoSth2(data)}).then((data)=>{
console.log('promise', data)
data.then((data)=>{
console.log('p2', data)// p2 20})})
优化
.then里递归resolve
functiondoSth(){let data =10let state ='pending'returnnewPromise((resolve, reject)=>{resolve(data)})}functiondoSth2(data){
console.log('doSth2', data)// 20returnnewPromise((resolve)=>{resolve(data)})}functionPromise(executor){let callback,
value
functionresolve(data){if(data instanceofPromise){
console.log('返回的是promise', data.then(resolve))}else{
value = data
}
state ='resolved'}functionreject(){}functionhandle(handler){if(!handler.cb){
handler.resolve(value)return}let res = handler.cb(value)
handler.resolve(res)}executor(resolve)this.then=(cb)=>{returnnewPromise(function(resolve){handle({
resolve,
cb
})})}}let promise3 =doSth()doSth().then((data)=>{
console.log('p1', data)// p1 10return20}).then((data)=>{returndoSth2(data)}).then((data)=>{
console.log('promise2', data)})
失败的回调
成功还是失败是在doSth里决定的
改造.then
改造handle
functiondoSth(){returnnewPromise((resolve, reject)=>{let reason ='手动失败'reject(reason)})}functionPromise(executor){let callback,
value,
state ='pending'functionresolve(data){if(data instanceofPromise){
console.log('返回的是promise', data.then(resolve))}else{
value = data
}
state ='resolved'}functionreject(reason){
state ='rejected'
value = reason
}functionhandle(handler){let handlerCallback
if(state ==='resolved'){
handlerCallback = handler.cb
}else{
handlerCallback = handler.cbReject
}if(!handlerCallback){if(state ==='resolved'){
handler.resolve(value)}else{
handler.reject(value)}return}let res =handlerCallback(value)
handler.resolve(res)}executor(resolve, reject)this.then=(cb, cbReject)=>{returnnewPromise(function(resolve, reject){handle({
cb,
resolve,
cbReject,
reject,})})}}let promise3 =doSth()doSth().then((data)=>{
console.log('p1', data)return20},(err)=>{
console.log('err', err)// err 手动失败})
优化
对内部resolve做异常处理 try…catch
对.then的handlerCallback(value)做异常处理
promise应当是异步的:用定时器包裹handle
functiondoSth(){returnnewPromise((resolve, reject)=>{let reason ='手动失败'reject(reason)})}functionPromise(executor){let callback,
value,
state ='pending'functionresolve(data){try{if(data instanceofPromise){
console.log('返回的是promise', data.then(resolve))}else{
value = data
}
state ='resolved'}catch(e){reject(e)}}functionreject(reason){
state ='rejected'
value = reason
}functionhandle(handler){setTimeout(()=>{let handlerCallback
if(state ==='resolved'){
handlerCallback = handler.cb
}else{
handlerCallback = handler.cbReject
}if(!handlerCallback){if(state ==='resolved'){
handler.resolve(value)}else{
handler.reject(value)}return}let res
try{
res =handlerCallback(value)}catch(e){reject(e)return}
handler.resolve(res)});}executor(resolve, reject)this.then=(cb, cbReject)=>{returnnewPromise(function(resolve, reject){handle({
cb,
resolve,
cbReject,
reject,})})}}let promise3 =doSth()
console.log(1)doSth().then((data)=>{
console.log('p1', data)return20},(err)=>{
console.log('err', err)// err 手动失败})
console.log(2)