Giter VIP home page Giter VIP logo

fake-promise's Introduction

ismorphic-promise. promise异步处理的JS实现。

build status

项目特点

  1. 通过了MDN官网的Promise用例

  2. 源代码基于ES6的class语法,更加清晰

Promise设计的基本框架

  1. 数据结构

采用链表的数据结构来描述Promise的状态

then/catch方法抽象为树形链表的一个节点(可以类比dom tree)

Promise抽象为状态的发起者,向所有的子节点触发事件,每条branch都只有一个回应者,依次递归。

  1. 算法

树状节点的状态更新来自以下两方面.

  1. resolved,rejected修改Promise,并异步传递状态给子节点。

  2. then/catch的节点添加入链表,触发状态检查。

每个数据状态都依赖一个Promise来发起,并保持。

Promise数据结构骨架

     let PromiseStructure = {

        promiseState:{ //存储数据及状态
            status:'',
            value:''
        },

        sealed:boolean, //Promise是否已有状态,有的话就封锁

        next:[
            // then/catch节点
        ]

        then,

        catch
    }

then/catch返回节点的数据结构

     let then/catch节点 = {

        fn,   //处理函数

        next:[
            // then/catch节点
        ],

        type, //表明是处理then/catch的函数

        then, //链式API

        catch //链式API
    }

基本的Promise流程

fake-promise's People

Contributors

slashhuang avatar

Stargazers

renaesop avatar KMBaby avatar  avatar

Watchers

James Cloos avatar  avatar

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.