Giter Site home page Giter Site logo

custompromise's Introduction

customPromise

手撕promise

20211104

type: 手撕第一版本 0.0.1 start

name: CustomPromise

实现的功能:

1、CustomPromise构造函数立即执行、定义 status、data、callbacks、resolve、reject、executor(resolve, reject)

2、原型对象上then方法(3种情况的判断 pending resolved rejected )

3、函数内resolve方法

未实现的功能: Promise.all、Promise.race、Promise.reject、Promise.resolve、实现catch方法

new Promise((resolve, reject) => {
  resolve(1)

}).then(res => {
  console.log(res)
}).then(res => {
  console.log(res)
})

// 答案
1 
undefined


new Promise((resolve, reject) => {
  resolve(1)

}).then(res => {
  return new Promise((resolve, reject) => {
    resolve(100)
  })
}).then(res => {
  console.log(res)
})
// 答案
100

type: 手撕第一版本 0.0.1 end

20211104

type: 手撕第一版本 0.0.2 start

name: CustomPromise

实现的功能:

1、CustomPromise构造函数立即执行、定义 status、data、callbacks、resolve、reject、executor(resolve, reject)

2、原型对象上then方法(3种情况的判断 pending resolved rejected )

3、函数内resolve方法

未实现的功能: Promise.all、Promise.race、Promise.reject、Promise.resolve、实现catch方法

将冗余的代码封装承方法

      function handle(callback) {
        try {
          let result = callback(self.data)
          if(result instanceof CustomPromise) {
            result.then( value => {resolve(value)}, reason => {reject(reason)})
          } else {
            resolve(result);
          }
        } catch (e) {
          reject(e)
        }
      }

type: 手撕第一版本 0.0.2 end


type: 手撕第一版本 0.0.3 start

name: CustomPromise

实现的功能:

1、CustomPromise构造函数立即执行、定义 status、data、callbacks、resolve、reject、executor(resolve, reject)

2、原型对象上then方法(3种情况的判断 pending resolved rejected )

3、函数内resolve方法

4、Promise.all、Promise.race、Promise.reject、Promise.resolve、实现catch方法

5、习题:sequenceTasksWithPromise(多个promise按顺序执行 reduce版本)- sequenceTasksWithPromise.js - 完成

6、习题:多个promise按顺序执行习题 -(for循环版本) - 待完成

7、习题:多个promise按顺序执行习题 - sequenceText.js - 待完成

未实现的功能:

待验证 Promise.all、Promise.race、Promise.reject、Promise.resolve、实现catch方法

type: 手撕第一版本 0.0.3 end


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.