zoukankan      html  css  js  c++  java
  • Promise 用es5的基础实现

    只实现 then 和 catch

    function promise(fn) {
      var state = 'pending';
      // 声明函数
      var nowResolve = function (arg) { return arg };
      var nowReject = function (arg) { return arg };
      var nextResolve = function (arg) { return arg };
      var nextReject = function (arg) { return arg };
      var catchReject = function (e) { throw e + ' (in promise)' };
    
      promise.prototype.then = function (res, rej) {
        typeof res === 'function' ? nowResolve = res : '';
        typeof rej === 'function' ? nowReject = rej : '';
        // return 新的对象
        return new promise(function (resolve, reject) {
          // then 中 return 的值传递给下一个 resolve/reject
          nextResolve = resolve;
          // 如果 then 中有 reject 回调函数, 则将 return 的值传递给下一个 resolve, 否则继续传递给reject
          nextReject = typeof rej === 'function' ? resolve : reject;
          // 捕获错误的回调函数
          catchReject = reject;
        });
      }
    
      promise.prototype.catch = function (fn) {
        return this.then(null, fn);
      }
    
      // 传值到下一个回调,以及异常捕获
      function tryCatchFn(state, arg) {
        try {
          state === 'fulfilled' ? nextResolve(nowResolve(arg)) : nextReject(nowReject(arg));
        } catch (e) {
          catchReject(e);
        }
      }
    
      function callback(value) {
        return function (arg) {
          if (state !== 'pending') { return; }
          state = value;
          // 如果传参是 promise 构造器生成的对象,传递对象 resolve 的值
          if (arg instanceof promise) {
            arg.then(function (res) {
              tryCatchFn('fulfilled', res);
            }, function (rej) {
              tryCatchFn('rejected', rej);
            });
            return;
          }
          // 如果是普通的传值,setTimeout 是为了 resolve/reject 同步代码的时候正常工作
          setTimeout(function () {
            tryCatchFn(state, arg);
          });
        }
      }
    
      fn(
        callback('fulfilled'),
        callback('rejected')
      );
    }
    

      

  • 相关阅读:
    读懂Netty的高性能架构之道
    大型网站架构演变和知识体系(转载)
    SAX,功能强大的 API
    防雪崩利器:熔断器 Hystrix 的原理与使用
    分布式系统设计系列 -- 基本原理及高可用策略
    分布式系统的事务处理
    分布式服务框架之服务化最佳实践
    深入理解 Java 虚拟机:JVM 高级特性与最佳实践
    内存屏障
    IntelliJ IDEA 2016 破解旗舰版
  • 原文地址:https://www.cnblogs.com/NKnife/p/8806490.html
Copyright © 2011-2022 走看看