Promise(异步处理-实现)

// 基础版

function MyPromise(fn) {
     
    let self = this; // 缓存当前promise实例
    self.value = null; //成功时的值
    self.error = null; //失败时的原因
    self.onFulfilled = null; //成功的回调函数
    self.onRejected = null; //失败的回调函数

    function resolve(value) {
     
        self.value = value;
        self.onFulfilled(self.value);//resolve时执行成功回调
    }

    function reject(error) {
     
        self.error = error;
        self.onRejected(self.error)//reject时执行失败回调
    }

    fn(resolve, reject);
}

MyPromise.prototype.then = function(onFulfilled, onRejected) {
     
    //在这里给promise实例注册成功和失败回调
    this.onFulfilled = onFulfilled;
    this.onRejected = onRejected;
}

module.exports = MyPromise;

// 不支持同步任务
let promise = new Promise((resolve, reject) => {
     
    resolve("同步任务执行")
});
// 基础版不支持同步任务,要支持同步任务,需要改成如下


function resolve(value) {
     
    //利用setTimeout特性将具体执行放到then之后
    setTimeout(() => {
     
        self.value = value;
        self.onFulfilled(self.value)
    })
}

function reject(error) {
     
    setTimeout(() => {
     
        self.error = error;
        self.onRejected(self.error)
    })
}
// 支持三种状态

//定义三种状态
const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";

function MyPromise(fn) {
     
    let self = this;
    self.value = null;
    self.error = null;
    self.status = PENDING;
    self.onFulfilled = null;
    self.onRejected = null;

    function resolve(value) {
     
        //如果状态是pending才去修改状态为fulfilled并执行成功逻辑
        if (self.status === PENDING) {
     
            setTimeout(function() {
     
                self.status = FULFILLED;
                self.value = value;
                self.onFulfilled(self.value);
            })
        }
    }

    function reject(error) {
     
        //如果状态是pending才去修改状态为rejected并执行失败逻辑
        if (self.status === PENDING) {
     
            setTimeout(function() {
     
                self.status = REJECTED;
                self.error = error;
                self.onRejected(self.error);
            })
        }
    }
    fn(resolve, reject);
}

MyPromise.prototype.then = function(onFulfilled, onRejected) {
     
    if (this.status === PENDING) {
     
        this.onFulfilled = onFulfilled;
        this.onRejected = onRejected;
    } else if (this.status === FULFILLED) {
     
        //如果状态是fulfilled,直接执行成功回调,并将成功值传入
        onFulfilled(this.value)
    } else {
     
        //如果状态是rejected,直接执行失败回调,并将失败原因传入
        onRejected(this.error)
    }
    return this;
}

module.exports = MyPromise
// 支持链式操作 暂未完成

const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";


function MyPromise(fn) {
     
    let self = this;
    self.value = null;
    self.error = null;
    self.status = PENDING;
     self.onFulfilled = null;
    self.onRejected = null;

    self.onFulfilledCallbacks = [];
    self.onRejectedCallbacks = [];


    function resolve(value) {
     
        //如果状态是pending才去修改状态为fulfilled并执行成功逻辑
        if (self.status === PENDING) {
     
            setTimeout(function() {
     
                self.status = FULFILLED;
                self.value = value;
                // self.onFulfilled(self.value);
                self.onFulfilledCallbacks.forEach((callback) => callback(self.value));
            })
        }
    }


    function reject(error) {
     
        //如果状态是pending才去修改状态为rejected并执行失败逻辑
        if (self.status === PENDING) {
     
            setTimeout(function() {
     
                self.status = REJECTED;
                self.error = error;
                // self.onRejected(self.error);
                self.onRejectedCallbacks.forEach((callback) => callback(self.value));
            })
        }
    }
    fn(resolve, reject);
}


MyPromise.prototype.then = function(onFulfilled, onRejected) {
     
    if (this.status === PENDING) {
     
        this.onFulfilledCallbacks.push(onFulfilled);
        this.onRejectedCallbacks.push(onRejected);
    } else if (this.status === FULFILLED) {
     
        onFulfilled(this.value)
    } else {
     
        onRejected(this.error)
    }
    return this;
}

module.exports = MyPromise
// Promises/A+规范

const PENDING = "pending";
const FULFILLED = "fulfilled";
const REJECTED = "rejected";


function MyPromise(fn) {
     
    const self = this;
    self.value = null;
    self.error = null;
    self.status = PENDING;
    self.onFulfilledCallbacks = [];
    self.onRejectedCallbacks = [];


    function resolve(value) {
     
        if (value instanceof MyPromise) {
     
            return value.then(resolve, reject);
        }
        if (self.status === PENDING) {
     
            setTimeout(() => {
     
                self.status = FULFILLED;
                self.value = value;
                self.onFulfilledCallbacks.forEach((callback) => callback(self.value));
            }, 0)
        }
    }


    function reject(error) {
     
        if (self.status === PENDING) {
     
            setTimeout(function() {
     
                self.status = REJECTED;
                self.error = error;
                self.onRejectedCallbacks.forEach((callback) => callback(self.error));
            }, 0)
        }
    }
    try {
     
        fn(resolve, reject);
    } catch (e) {
     
        reject(e);
    }
}


function resolvePromise(bridgepromise, x, resolve, reject) {
     
    //2.3.1规范,避免循环引用
    if (bridgepromise === x) {
     
        return reject(new TypeError('Circular reference'));
    }
    let called = false;
    //这个判断分支其实已经可以删除,用下面那个分支代替,因为promise也是一个thenable对象
    if (x instanceof MyPromise) {
     
        if (x.status === PENDING) {
     
            x.then(y => {
     
                resolvePromise(bridgepromise, y, resolve, reject);
            }, error => {
     
                reject(error);
            });
        } else {
     
            x.then(resolve, reject);
        }
        // 2.3.3规范,如果 x 为对象或者函数
    } else if (x != null && ((typeof x === 'object') || (typeof x === 'function'))) {
     
        try {
     
            // 是否是thenable对象(具有then方法的对象/函数)
            //2.3.3.1 将 then 赋为 x.then
            let then = x.then;
            if (typeof then === 'function') {
     
            //2.3.3.3 如果 then 是一个函数,以x为this调用then函数,且第一个参数是resolvePromise,第二个参数是rejectPromise
                then.call(x, y => {
     
                    if (called) return;
                    called = true;
                    resolvePromise(bridgepromise, y, resolve, reject);
                }, error => {
     
                    if (called) return;
                    called = true;
                    reject(error);
                })
            } else {
     
            //2.3.3.4 如果 then不是一个函数,则 以x为值fulfill promise。
                resolve(x);
            }
        } catch (e) {
     
        //2.3.3.2 如果在取x.then值时抛出了异常,则以这个异常做为原因将promise拒绝。
            if (called) return;
            called = true;
            reject(e);
        }
    } else {
     
        resolve(x);
    }
}


MyPromise.prototype.then = function(onFulfilled, onRejected) {
     
    const self = this;
    let bridgePromise;
    onFulfilled = typeof onFulfilled === "function" ? onFulfilled : value => value;
    onRejected = typeof onRejected === "function" ? onRejected : error => {
      throw error };
    if (self.status === FULFILLED) {
     
        return bridgePromise = new MyPromise((resolve, reject) => {
     
            setTimeout(() => {
     
                try {
     
                    let x = onFulfilled(self.value);
                    resolvePromise(bridgePromise, x, resolve, reject);
                } catch (e) {
     
                    reject(e);
                }
            }, 0);
        })
    }
    if (self.status === REJECTED) {
     
        return bridgePromise = new MyPromise((resolve, reject) => {
     
            setTimeout(() => {
     
                try {
     
                    let x = onRejected(self.error);
                    resolvePromise(bridgePromise, x, resolve, reject);
                } catch (e) {
     
                    reject(e);
                }
            }, 0);
        });
    }
    if (self.status === PENDING) {
     
        return bridgePromise = new MyPromise((resolve, reject) => {
     
            self.onFulfilledCallbacks.push((value) => {
     
                try {
     
                    let x = onFulfilled(value);
                    resolvePromise(bridgePromise, x, resolve, reject);
                } catch (e) {
     
                    reject(e);
                }
            });
            self.onRejectedCallbacks.push((error) => {
     
                try {
     
                    let x = onRejected(error);
                    resolvePromise(bridgePromise, x, resolve, reject);
                } catch (e) {
     
                    reject(e);
                }
            });
        });
    }
}

MyPromise.prototype.catch = function(onRejected) {
     
    return this.then(null, onRejected);
}

// 执行测试用例需要用到的代码
MyPromise.deferred = function() {
     
    let defer = {
     };
    defer.promise = new MyPromise((resolve, reject) => {
     
        defer.resolve = resolve;
        defer.reject = reject;
    });
    return defer;
}
try {
     
    module.exports = MyPromise
} catch (e) {
     }
// 继续实现all,race,resolve,reject方法

MyPromise.all = function(promises) {
     
    return new MyPromise(function(resolve, reject) {
     
        let result = [];
        let count = 0;
        for (let i = 0; i < promises.length; i++) {
     
            promises[i].then(function(data) {
     
                result[i] = data;
                if (++count == promises.length) {
     
                    resolve(result);
                }
            }, function(error) {
     
                reject(error);
            });
        }
    });
}


MyPromise.race = function(promises) {
     
    return new MyPromise(function(resolve, reject) {
     
        for (let i = 0; i < promises.length; i++) {
     
            promises[i].then(function(data) {
     
                resolve(data);
            }, function(error) {
     
                reject(error);
            });
        }
    });
}


MyPromise.resolve = function(value) {
     
    return new MyPromise(resolve => {
     
        resolve(value);
    });
}


MyPromise.reject = function(error) {
     
    return new MyPromise((resolve, reject) => {
     
        reject(error);
    });
}
// 增加 promisify方法,作用就是 把callback的写法,改成 .then调用的方式,使用;

MyPromise.promisify = function(fn) {
     
    return function() {
     
        var args = Array.from(arguments);
        return new MyPromise(function(resolve, reject) {
     
            fn.apply(null, args.concat(function(err) {
     
                err ? reject(err) : resolve(arguments[1])
            }));
        })
    }
}
// Promise实现

function Promise(fn) {
     
  let state = 'pending'
  let value = null
  const callbacks = []

  this.then = function (onFulfilled, onRejected) {
     
    return new Promise((resolve, reject) => {
     
      handle({
     
        onFulfilled,
        onRejected,
        resolve,
        reject,
      })
    })
  }

  this.catch = function (onError) {
     
    return this.then(null, onError)
  }


  this.finally = function (onDone) {
     
    this.then(onDone, onError)
  }


  this.resolve = function (value) {
     
    if (value && value instanceof Promise) {
     
      return value
    } if (value && typeof value === 'object' && typeof value.then === 'function') {
     
      const {
      then } = value
      return new Promise((resolve) => {
     
        then(resolve)
      })
    } if (value) {
     
      return new Promise(resolve => resolve(value))
    }
    return new Promise(resolve => resolve())
  }


  this.reject = function (value) {
     
    return new Promise(((resolve, reject) => {
     
      reject(value)
    }))
  }


  this.all = function (arr) {
     
    const args = Array.prototype.slice.call(arr)
    return new Promise(((resolve, reject) => {
     
      if (args.length === 0) return resolve([])
      let remaining = args.length


      function res(i, val) {
     
        try {
     
          if (val && (typeof val === 'object' || typeof val === 'function')) {
     
            const {
      then } = val
            if (typeof then === 'function') {
     
              then.call(val, (val) => {
     
                res(i, val)
              }, reject)
              return
            }
          }
          args[i] = val
          if (--remaining === 0) {
     
            resolve(args)
          }
        } catch (ex) {
     
          reject(ex)
        }
      }
      for (let i = 0; i < args.length; i++) {
     
        res(i, args[i])
      }
    }))
  }


  this.race = function (values) {
     
    return new Promise(((resolve, reject) => {
     
      for (let i = 0, len = values.length; i < len; i++) {
     
        values[i].then(resolve, reject)
      }
    }))
  }


  function handle(callback) {
     
    if (state === 'pending') {
     
      callbacks.push(callback)
      return
    }
    const cb = state === 'fulfilled' ? callback.onFulfilled : callback.onRejected
    const next = state === 'fulfilled' ? callback.resolve : callback.reject
    if (!cb) {
     
      next(value)
      return
    }    
    let ret;
    try {
     
     ret = cb(value)
    } catch (e) {
     
      callback.reject(e)
    }
    callback.resolve(ret);
  }

  function resolve(newValue) {
     
    const fn = () => {
     
      if (state !== 'pending') return


      if (newValue && (typeof newValue === 'object' || typeof newValue === 'function')) {
     
        const {
      then } = newValue
        if (typeof then === 'function') {
     
          // newValue 为新产生的 Promise,此时resolve为上个 promise 的resolve
          // 相当于调用了新产生 Promise 的then方法,注入了上个 promise 的resolve 为其回调
          then.call(newValue, resolve, reject)
          return
        }
      }
      state = 'fulfilled'
      value = newValue
      handelCb()
    }
    setTimeout(fn, 0)
  }

  function reject(error) {
     
    const fn = () => {
     
      if (state !== 'pending') return

      if (error && (typeof error === 'object' || typeof error === 'function')) {
     
        const {
      then } = error
        if (typeof then === 'function') {
     
          then.call(error, resolve, reject)
          return
        }
      }
      state = 'rejected'
      value = error
      handelCb()
    }
    setTimeout(fn, 0)
  }

  function handelCb() {
     
    while (callbacks.length) {
     
      const fn = callbacks.shift()
      handle(fn)
    }
  }

  try {
     
    fn(resolve, reject)
  } catch(ex) {
     
    reject(ex);
  }
}
// promise极简版

function promise () {
     
  this.status = 'pending' // 2.1
  this.msg = '' // 存储value与reason
  let process = arguments[0],
       that = this
  process (function () {
     
    that.status = 'resolve'
    that.msg = argument[0]
  }, function () {
     
    that.status = 'reject'
    that.msg = argument[0]
  })
  return this
}

promise.prototype.then = function () {
     
  if (this.status === 'resolve') {
     
    arguments[0](this.msg)
  } else if (this.status === 'reject' && arguments[1]) {
     
    arguments[1](this.msg)
  }
}

你可能感兴趣的:(Javascript,javascript,typescript,前端,node.js)