五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

手寫Promise核心代碼 - JavaScript前端Web工程師

2022-05-07 23:48 作者:江火漁楓  | 我要投稿

```

const { reject } = require("core-js/fn/promise");


class Commitment {


??static PENDING = '待定';

??static FULFILLED = '成功';

??static REJECTED = '拒絕';


??constructor(func){

????this.status = Commitment.PENDING;

????this.result = null;

????this.resolveCallbacks = [];

????this.rejectCallbacks = [];

????//***bind(this) 將this指向新創(chuàng)建的對象 commitment ****/

????//func(this.resolve.bind(this), this.reject.bind(this));


????//原生promise調(diào)用than 可以把錯誤的信息做為內(nèi)容輸出出來 調(diào)用方式如下

????/***

?????* let promise = new Promise((resolve, reject) => {

?????*?throw new Error('輸出錯誤信息');

?????* })

?????* ** */

????try{

??????func(this.resolve.bind(this), this.reject.bind(this));

????} catch (error){

??????//這里是直接執(zhí)行不是創(chuàng)建實例后再執(zhí)行 不用綁定this

??????this.reject(error);

????}

??}


??resolve(){

????setTimeout(() => {

??????if(this.status === Commitment.PENDING){

????????this.status = Commitment.FULFILLED;

????????this.result = result;

????????this.resolveCallbacks.forEach(callback =>{

??????????callback(result)

????????})

??????}

????});

??}

??reject(){

????setTimeout(() => {

??????if(this.status === Commitment.PENDING){

????????this.status = Commitment.REJECTED;

????????this.result = result;

????????this.rejectCallbacks.forEach(callback => {

??????????callback(result)

????????})

??????}

????});

??}

??than(onFULFILLED, onREJECTED){

????//為實現(xiàn) .than鏈?zhǔn)秸{(diào)用?方法內(nèi)返回new Commitment

????return new Commitment((resolve, reject) => {

??????// than 里面的兩個參數(shù)不是函數(shù)則不被執(zhí)行

??????//解決思路 賦值空函數(shù)

??????onFULFILLED = typeof onFULFILLED === 'function' ? onFULFILLED : () => {};

??????onREJECTED = typeof onREJECTED === 'function' ? onREJECTED : () => {};

??????if(this.status === Commitment.PENDING){

????????this.resolveCallbacks.push(onFULFILLED);

????????this.rejectCallbacks.push(onREJECTED);

??????}

??????if(this.status === Commitment.FULFILLED){

????????onFULFILLED(this.result);

??????}

??????if(this.status === Commitment.REJECTED){

????????onREJECTED(this.result);

??????}

????})

??}

}


let commitment = new Commitment((resolve, reject) => {

??resolve('這次一定');

});

commitment.than(

??result => {console.log(result)},

??result => {console.log(result.message)}

).than(

??result => {console.log(result)},

??result => {console.log(result.message)}

);


```






手寫Promise核心代碼 - JavaScript前端Web工程師的評論 (共 條)

分享到微博請遵守國家法律
金坛市| 鹰潭市| 拜城县| 钟山县| 比如县| 梧州市| 新化县| 郁南县| 平南县| 揭阳市| 金阳县| 南乐县| 邵武市| 依兰县| 历史| 罗平县| 年辖:市辖区| 深州市| 顺平县| 佛坪县| 深圳市| 盐城市| 台湾省| 合川市| 景泰县| 开平市| 西乌| 合肥市| 容城县| 襄樊市| 敦化市| 广州市| 洪洞县| 泸州市| 乃东县| 彭山县| 连江县| 甘孜| 牟定县| 醴陵市| 襄城县|