美文网首页
手写Promise

手写Promise

作者: 梦不成 | 来源:发表于2019-06-11 12:22 被阅读0次

基本实现new Promise 和 then /catch方法

  • Promise.js
class Promise {
  constructor(excutorCallback) {
    this.status = 'pending' //new Primise 时为进行中
    this.value = undefined //resolve/reject的初始值(参数)
    
    this.fulfilledAry = [] //成功之后要做的事
    this.rejectedAry = [] //失败之后要做的事

    let resolveFn = result => {
      let timer = setTimeout(() => {
        clearTimeout(timer)  
        if (this.status !== 'pending') return
        this.status = 'fulfilled'
        this.value = result

        this.fulfilledAry.forEach(item => {
          item(this.value)
        })
        }, 0)
    }

    let rejectFn = reason => {
      let timer = setTimeout(() => {
        clearTimeout(timer)
        if (this.status !== 'pending') return
        this.status = 'rejected'
        this.value = reason

        this.rejectedAry.forEach(item => {
          item(this.value)
        })
      }, 0)
    }

    //实例化时异常捕获
    try {
      excutorCallback(resolveFn, rejectFn)
    } catch (err) {
      //异常按rejected状态处理
      rejectFn(err)
    }
  }

  //原型上加方法
  then (fulfilledCallback, rejectedCallback) {
    //then的参数不传的情况 给它一个函数以承接数据 在后面的then里面就可以拿到了
    typeof fulfilledCallback !== 'function' ? fulfilledCallback = result => result : null

    typeof rejectedCallback !== 'function' ? rejectedCallback = reason => {
      throw new Error(reason.message)
    } : null

    //链式操作返回一个新的Promise实例
    return new Promise((resolve, reject) => {
      this.fulfilledAry.push(() => {
        try {
          let x = fulfilledCallback(this.value)
          x instanceof Promise ? x.then(resolve, reject) : resolve(x)
        } catch (err) {
          reject(err)
        }
        
      })
      this.rejectedAry.push(() => {

        try {
          let x = rejectedCallback(this.value)
          x instanceof Promise ? x.then(resolve, reject) : resolve(x)
        } catch (err) {
          reject(err)
        }
      })
    })
  }

  //原型上添加catch方法
  catch (rejectedCallback) {
    return this.then(null, rejectedCallback)
  }
}

module.exports = Promise
  • test.js测试 调用手写的Promise函数
const Promise = require('./Promise')

new Promise((resolve, reject) => {

  // throw new Error('wrong')
  resolve(100)

  reject(-100)

}).then((result) => {
  console.log(result);
  console.log('成功')
  
}, (reason) => {
  console.log(reason)
  console.log('失败')

})

console.log('同步方法');

相关文章

  • 手写Promise

    手写 Promise 我们会通过手写一个符合 Promise/A+ 规范的 Promise 来深入理解它,并且手写...

  • 手写 Promise 系列 --- 3

    在前两篇(手写 Promise 系列 --- 1)和(手写 Promise 系列 ---2) 中,达成了3个目标 ...

  • 手写Promise

    $ 正常的promise用法   $ 手写的Promise   # 测试可行性

  • 手写promise

    手写promise 带大家手写一个 promis。在手写之前我会先简单介绍一下为什么要使用promise、prom...

  • 纯手写实现自己的nodejs promise 库

    纯手写实现自己的nodejs promise 库什么是Promise?promise 链Async/Await后续...

  • 手写基础 promise

    1. 前言 玩下吧 手写 promise,看看能写成啥样 2. promise 基础结构 3. 手写`promi...

  • 手写 Promise

    一、Promise 是一个异步操作返回的对象,用来传递异步操作的消息。 Promise 介绍和使用详见: 认识并使...

  • 手写Promise

  • 手写Promise

    Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大。 这篇博客有关于P...

  • 手写promise

    本章节纯粹是对promise手写实现。如果不了解promise自行了解再来学习本章知识。promise初体验首先,...

网友评论

      本文标题:手写Promise

      本文链接:https://www.haomeiwen.com/subject/clnffctx.html