Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何实现Promise? #11

Open
Rain120 opened this issue Jun 14, 2019 · 0 comments
Open

如何实现Promise? #11

Rain120 opened this issue Jun 14, 2019 · 0 comments

Comments

@Rain120
Copy link
Owner

Rain120 commented Jun 14, 2019

News

手写 Promise


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

function Promise(executor) {
    let that = this;
    that.status = PENDING;
    that.value = undefined;
    that.reason = undefined;
    that.onFulfilledCallbacks = [];
    that.onRejectedCallbacks = [];

    function resolve(value) {
        if (value instanceof Promise) {
            return value.then(resolve, reject);
        }
        setTimeout(() => {
            if (that.status === PENDING) {
                that.status = FULFILLED;
                that.value = value;
                that.onFulfilledCallbacks.forEach(cb => cb(that.value));
            }
        });
    }

    function reject(reason) {
        setTimeout(() => {
            if (that.status === PENDING) {
                that.status = REJECTED;
                that.reason = reason;
                that.onRejectedCallbacks.forEach(cb => cb(that.reason));
            }
        });
    }
    try {
        executor(resolve, reject);
    } catch (e) {
        reject(e);
    }
}
// then, 如果状态是pending,那就加入不同状态的执行函数列表中
Promise.prototype.then = function (onFulfilled, onRejected) {
    const that = this;
    var newPromise;
    onFulfilled =
        typeof onFulfilled === "function" ? onFulfilled : value => value;
    onRejected =
        typeof onRejected === "function" ? onRejected : reason => reason;
    if (that.status === FULFILLED) {
        return newPromise = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    let x = onFulfilled(that.value);
                    resolvePromise(newPromise, x, resolve, reject);
                } catch (e) {
                    reject(e);
                }
            });
        })
    }

    if (that.status === REJECTED) {
        return newPromise = new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    let x = onRejected(that.reason);
                    resolvePromise(newPromise, x, resolve, reject);
                } catch (e) {
                    reject(e);
                }
            });
        });
    }

    if (that.status === PENDING) {
        return newPromise = new Promise((resolve, reject) => {
            that.onFulfilledCallbacks.push((value) => {
                try {
                    let x = onFulfilled(value);
                    resolvePromise(newPromise, x, resolve, reject);
                } catch (e) {
                    reject(e);
                }
            });
            that.onRejectedCallbacks.push((reason) => {
                try {
                    let x = onRejected(reason);
                    resolvePromise(newPromise, x, resolve, reject);
                } catch (e) {
                    reject(e);
                }
            });
        });
    }
};
// 解析resolve是否是promise
function resolvePromise(newPromise, x, resolve, reject) {
    if (newPromise == x) {
        return reject(new TypeError('Chaining cycle'));
    }
    var called;
    if (x !== null && (typeof x === 'object' || typeof x === 'function')) {
        try {
            var then = x.then;
            if (typeof then === 'funcition') {
                then.call(x, next => {
                    if (called) {
                        return;
                    }
                    called = true;
                    resolvePromise(newPromise, next, resolve, reject);
                }, err => {
                    if (called) {
                        return;
                    }
                    called = true;
                    reject(err);
                });
            } else {
                resolve(x);
            }
        } catch (err) {
            if (called) {
                return;
            }
            called = true;
            reject(err);
        }
    }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant