使用ES6模擬Promise

2021-09-24 12:09:42 字數 1581 閱讀 3230

const promise =((

)=>

newstatus

* @param newvalue

* @param queue 執行的作業佇列

*/[changestatus]

(newstatus, newvalue, queue)

this

[promisestatus]

= newstatus;

this

[promivevalue]

= newvalue;

//執行相應佇列中的函式

queue.

foreach

(handler =>

handler

(newvalue));

}/**

* * @param executor 未決階段(pending狀態)下的處理函式

*/constructor

(executor)

const

reject

= reason =>

trycatch

(err)}

/** * 處理 後續處理函式

* @param handler 後續處理函式

* @param immediatelystatus 需要立即執行的狀態

* @param queue 作業佇列

*/[settlehandle]

(handler, immediatelystatus, queue)if(

this

[promisestatus]

=== immediatelystatus),0

);}else

}[linkpromise]

(thenalbe, catchable)

, err =>)}

else

}catch

(err)}

return

newpromise

((resolve, reject)

=>

,resolved

,this

[thenables]

)this

[settlehandle]

(reason =>

,rejected

,this

[catchables])}

)}then

(thenable, catchable)

catch

(catchable)

static

all(proms)

p.then

(data =>

}, reason =>

)return obj;})

})}static

race

(proms)

, err =>)}

)})}

static

resolve

(data)

else)}

}static

reject

(reason))}

}})(

);

ES6關於promise技巧

1.推向promise物件推向 不同的狀態 const pro newpromise resolve,reject 2.原型成員 例項成員 註冊乙個後續處理函式,當promise為resolved狀態是執行該函式,當promise為rejected狀態是執行該函式的第二個引數 catch 註冊乙個後...

ES6 學習 Promise物件

1.promise含義 可以將promise物件看成是乙個容器,它儲存著未來才會結束的某個事件 一般是非同步操作事件 的結果,各 種非同步操作都可以用promise物件來處理 promise的特點 1 promise物件有三個狀態 pending 進行中 fulfilled 已成功 和rejecte...

談談 ES6 的 Promise 物件

ajax 如果幾個非同步操作之間並沒有前後順序之分 例如不需要前乙個請求的結果作為後乙個請求的引數 時,同樣需要等待上乙個操作完成再實行下乙個操作。function helloworld ready else helloworld true then function message functio...