前端前端
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
fabric
算法
jsBlock
styleBlock
svgBlock
工具其他
Vue、React相关
webgl
GitHub
  • parseInt
  • preventDefault报错
  • promise
  • requestIdleCallback
  • undefined
  • webworker
  • 代码块
  • 前后端自动刷新
  • 多个标签页通讯
  • 实现个简单爬虫
  • 执行上下文
  • 控制台
  • 查找文件中的图片并下载
  • 深度广度优先
  • 累加函数
  • 触摸事件和键盘事件

promise

忘了哪里转的,找到再补

  const isFunction = (obj) => typeof obj === 'function'
  const isObject = (obj) => !!(obj && typeof obj === 'object')
  const isThenable = (obj) =>
    (isFunction(obj) || isObject(obj)) && 'then' in obj
  const isPromise = (promise) => promise instanceof Promise

  const PENDING = 'pending'
  const FULFILLED = 'fulfilled'
  const REJECTED = 'rejected'

  function Promise(f) {
    this.result = null
    this.state = PENDING
    this.callbacks = []

    let onFulfilled = (value) => transition(this, FULFILLED, value)
    let onRejected = (reason) => transition(this, REJECTED, reason)

    let ignore = false
    let resolve = (value) => {
      if (ignore) return
      ignore = true
      resolvePromise(this, value, onFulfilled, onRejected)
    }
    let reject = (reason) => {
      if (ignore) return
      ignore = true
      onRejected(reason)
    }

    try {
      f(resolve, reject)
    } catch (error) {
      reject(error)
    }
  }

  Promise.prototype.then = function (onFulfilled, onRejected) {
    return new Promise((resolve, reject) => {
      let callback = { onFulfilled, onRejected, resolve, reject }

      if (this.state === PENDING) {
        this.callbacks.push(callback)
      } else {
        setTimeout(() => handleCallback(callback, this.state, this.result), 0)
      }
    })
  }

  const handleCallback = (callback, state, result) => {
    let { onFulfilled, onRejected, resolve, reject } = callback
    try {
      if (state === FULFILLED) {
        isFunction(onFulfilled)
          ? resolve(onFulfilled(result))
          : resolve(result)
      } else if (state === REJECTED) {
        isFunction(onRejected) ? resolve(onRejected(result)) : reject(result)
      }
    } catch (error) {
      reject(error)
    }
  }

  const handleCallbacks = (callbacks, state, result) => {
    while (callbacks.length) handleCallback(callbacks.shift(), state, result)
  }

  const transition = (promise, state, result) => {
    if (promise.state !== PENDING) return
    promise.state = state
    promise.result = result
    setTimeout(() => handleCallbacks(promise.callbacks, state, result), 0)
  }

  const resolvePromise = (promise, result, resolve, reject) => {
    if (result === promise) {
      let reason = new TypeError('Can not fufill promise with itself')
      return reject(reason)
    }

    if (isPromise(result)) {
      return result.then(resolve, reject)
    }

    if (isThenable(result)) {
      try {
        let then = result.then
        if (isFunction(then)) {
          return new Promise(then.bind(result)).then(resolve, reject)
        }
      } catch (error) {
        return reject(error)
      }
    }

    resolve(result)
  }

  const t = new Promise(resolve => {
    setTimeout(() => {
      resolve('aaaa')
    }, 2000);
  })

  t.then(res => {
    console.log(res, 'rrrrr')
  })
Edit this page
最近更新:: 2021/5/26 17:52
Contributors: wuhui
Prev
preventDefault报错
Next
requestIdleCallback