自定義實現Promise

2021-10-05 23:56:41 字數 1481 閱讀 8305

promise是很重要的知識點,必須牢記於心,下面是自定義實現的promise:

class

mypromise

catch

(error)}

//成功時

resolve

(value))}

)}}//失敗時

reject

(reason))}

)}}then

(onresolve, onrejected)

)// p.then().then(value=>console.log(value)) //=>成功if(

typeof onresolve !==

"function")if

(typeof onrejected !==

"function"

)//返回的是mypromise物件

let promise =

newmypromise

((resolve, reject)

=>if(

this

.status === mypromise.

rejected

)// 如果resolve()是在settimeout()中進行的

// 如 let p = new promise((resolve, reject) => , 100);

// })

// p.then(value => console.log(value)) //=>成功

//需要把當前需要進行的函式儲存下來if(

this

.status === mypromise.

pending),

onrejected: value =>})

}})return promise;

}parse

(promise, result, resolve, reject)

try,

// reason =>

// )

//等於下面

result.

then

(resolve, reject);}

else

}catch

(error)}

)}static

resolve

(value)

else})

}static

reject

(value)

else})

}//當所有的promise都成功返回時才返回

static

all(promises)},

reason =>)}

)});

}//當有乙個promise成功返回時便返回這個promise

static

race

(promises)

, reason =>)}

)});

}}

自定義Toast實現自定義Toast布局

平時我們使用toast的時候都是這樣的一種方法 toast toast toast.maketext context,duration 現在我們來自定義下toast的布局,首先看下toast建立時的源 public static toast maketext context context,char...

實現自定義IFormattable

using system using system.collections using system.linq using system.text using system.collections.generic using system.runtime.serialization namespac...

自定義tabcontrol實現

using system using system.collections generic using system.drawing using system.linq using system.text using system.windows forms namespace shengxinso...