“约见”面试官系列之常见面试题之第八十四篇之手写promise(建议收藏)
生活随笔
收集整理的這篇文章主要介紹了
“约见”面试官系列之常见面试题之第八十四篇之手写promise(建议收藏)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class Promise{constructor(excutor){this.value = '';this.reason = '';this.status = 'padding'this.onFulfilledCallback = []this.onRejectedCallback = []let resolve = (value)=>{/*2.這個判斷是為了status不可逆 只能從 padding轉化為 成功或者失敗*/if (this.status == 'padding') {this.status = 'fulfilled'this.value = value/*3.當轉態改變的時候依次執行隊列里面儲存的then函數里面對應的回調*/this.onFulfilledCallback.forEach(fn=>{fn()})}};let reject = (reason)=>{/*2.這個判斷是為了status不可逆 只能從 padding轉化為 成功或者失敗*/if (this.status == 'padding') {this.status = 'rejected'this.reason = reason/*3.當轉態改變的時候依次執行隊列里面儲存的then函數里面對應的回調*/this.onRejectedCallback.forEach(fn=>{fn()})}};/*1. 當發生異常是捕獲異常 */try{excutor(resolve,reject)}catch (e){reject(e)}}then(onFulfilled,onRejected){//4.防止使用者不傳成功或失敗回調函數,所以成功失敗回調都給了默認回調函數onFulfilled = typeof onFulfilled === "function" ? onFulfilled : value => value;onRejected = typeof onRejected === "function" ? onRejected : error => { throw error };let newPromise;if(this.status == 'fulfilled'){return newPromise = new Promise((resolve,reject)=>{setTimeout(()=>{try{let x = onFulfilled(this.value)this.resolvePromise(newPromise, x, resolve, reject);}catch (e){reject(e)}})})}if(this.status == 'rejected'){return newPromise = new Promise((resolve,reject)=>{setTimeout(()=>{try{let x = onRejected(this.reason)this.resolvePromise(newPromise, x, resolve, reject);}catch (e){reject(e)}})})}if(this.status == 'padding'){return newPromise = new Promise((resolve,reject)=> {/*3.當excutor為異步的時候先把then方法里面的回調儲存在失敗或者成功的隊列里面*/this.onFulfilledCallback.push(() => {//這里可以寫其他的代碼對resolve做一層封裝todosetTimeout(()=> {try {let x = onFulfilled(this.value)this.resolvePromise(newPromise, x, resolve, reject);} catch (e) {reject(e)}})})this.onRejectedCallback.push(() => {setTimeout(()=> {try {let x = onRejected(this.reason)this.resolvePromise(newPromise, x, resolve, reject);} catch (e) {reject(e)}})})})}//4.保證鏈式調用 返回this 這樣做雖然能鏈式調用但是 所有鏈式調用的回調函數都掛載到同一個對象上 且當后面的then方法執行//的時候promise的狀態已經確定會馬上執行其對應的回調,并且參數都為this.value這一個值//return this}catch(onRejected){this.then(null,onRejected)}resolvePromise(newPromise,x,resolve,reject){//2.3.1規范,避免循環引用if (newPromise === x) {return reject(new TypeError('Circular reference'));}//called變量主要是用來判斷如果resolvePromise函數已經resolve或者reject了,那就不需要在執行下面的resolce或者reject。//設置一個標志位,在執行resolve或者reject其中之一后,講不能再執行resolve或者reject eg:resolve();reject()let called = false;if (x != null && ((typeof x === 'object') || (typeof x === 'function'))) {try{let then = x.then;if (typeof then === 'function') {//2.3.3.3 如果 then 是一個函數,以x為this調用then函數,且第一個參數是resolve,第二個參數是rejectthen.call(x, y => {if (called) return;called = true;this.resolvePromise(newPromise, y, resolve, reject);}, error => {if (called) return;called = true;reject(error);})} else {//2.3.3.4 如果 then不是一個函數,則 以x為值fulfill promise。resolve(x);}}catch(e){if (called) return;called = true;reject(e);}}else {resolve(x)}}}
// 執行測試用例需要用到的代碼
Promise.deferred = function() {let defer = {};defer.promise = new Promise((resolve, reject) => {defer.resolve = resolve;defer.reject = reject;});return defer;
}
try {module.exports = Promise
} catch (e) {}// 1.npm i -g promises-aplus-tests
// 2.promises-aplus-tests mypromise.js
本面試題為前端常考面試題,后續有機會繼續完善。我是歌謠,一個沉迷于故事的講述者。
歡迎一起私信交流。
“睡服“面試官系列之各系列目錄匯總(建議學習收藏)?
總結
以上是生活随笔為你收集整理的“约见”面试官系列之常见面试题之第八十四篇之手写promise(建议收藏)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 会计教学APP软件,实现教学的快速运转
- 下一篇: BackTrack 4 R2安装VMwa