zoukankan      html  css  js  c++  java
  • promise

    https://www.jianshu.com/p/c633a22f9e8c

    function Promise(executor) {
        var _this = this;
        this.state = 'pending';
        this.value = undefined;
        this.reason = undefined;
    
        executor(resolve, reject); //马上执行
        
        function resolve(value) {}
        function reject(reason) {}
        this.onFulfilledFunc = [];//保存成功回调
        this.onRejectedFunc = [];//保存失败回调
    }
        function resolve(value) {
            if (_this.state === 'pending') {
                _this.value = value;
                //依次执行成功回调
                _this.onFulfilledFunc.forEach(fn => fn(value));
                _this.state = 'resolved';
            }
    
        }
    
        function reject(reason) {
            if (_this.state === 'pending') {
                _this.reason = reason;
                //依次执行失败回调
                _this.onRejectedFunc.forEach(fn => fn(reason));
                _this.state = 'rejected';
            }
        }
    Promise.prototype.then = function (onFulfilled, onRejected) {
        //等待态,此时异步代码还没有走完
        if (this.state === 'pending') {
            if (typeof onFulfilled === 'function') {
                this.onFulfilledFunc.push(onFulfilled);//保存回调
            }
            if (typeof onRejected === 'function') {
                this.onRejectedFunc.push(onRejected);//保存回调
            }
        }
        //其它代码略...
    };
    Promise.prototype.then = function (onFulfilled, onRejected) {
        var promise2 = new Promise((resolve, reject) => {
        //代码略...
        }
        return promise2;
    };

     

  • 相关阅读:
    如何找bug
    信号量
    带组装的测试
    Oracle的一些操作
    NPOI的操作
    初次认识 C# win32 api
    C# 通过Selecnuim WebDriver操作非IE浏览器
    DevExpress.chartControt画趋势图
    DevExpress.chartContro控件保存图片和打印图片
    SqlServer基础复习
  • 原文地址:https://www.cnblogs.com/luyingying/p/13260127.html
Copyright © 2011-2022 走看看