{"id":2039,"date":"2025-10-16T21:13:16","date_gmt":"2025-10-16T13:13:16","guid":{"rendered":"https:\/\/womeifei.cn\/?p=2039"},"modified":"2025-10-16T22:11:17","modified_gmt":"2025-10-16T14:11:17","slug":"promise02-%e6%89%8b%e5%86%99","status":"publish","type":"post","link":"https:\/\/womeifei.cn\/index.php\/2025\/10\/16\/promise02-%e6%89%8b%e5%86%99\/","title":{"rendered":"Promise02 &#8212; \u624b\u5199"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u6784\u9020\u5668<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const PENDING = \"pending\"\nconst FULFILLED = \"fulfilled\"\nconst REJECTED = \"rejected\"\n\nclass MyPromise {\n    #state = PENDING\n    #value = undefined\n    constructor(executor) {\n        const resolve = (val) => {\n            this.#setState(FULFILLED, val)\n        }\n        const reject = (reason) => {\n            this.#setState(REJECTED, reason)\n        }\n        try {\n            executor(resolve, reject)\n        }\n        catch (err) {\n            reject(err)\n        }\n    }\n    #setState(state, value) {\n        if (this.#state !== PENDING) return\n        this.#value = value\n        this.#state = state\n        console.log(this.#state, this.#value)\n    }\n}\n\nconst p = new MyPromise((resolve, reject) => {\n    reject(2)\n    resolve(1)\n})<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.Promise.then()<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>const PENDING = \"pending\"\nconst FULFILLED = \"fulfilled\"\nconst REJECTED = \"rejected\"\n\nfunction isPromiseLike(obj) {\n    return typeof obj?.then === 'function'\n}\n\nclass MyPromise {\n    #state = PENDING\n    #value = undefined\n    #handlers = &#91;]\n    constructor(executor) {\n        const resolve = (val) => {\n            this.#setState(FULFILLED, val)\n        }\n        const reject = (reason) => {\n            this.#setState(REJECTED, reason)\n        }\n        try {\n            executor(resolve, reject)\n        }\n        catch (err) {\n            reject(err)\n        }\n    }\n    #setState(state, value) {\n        if (this.#state !== PENDING) return\n        this.#value = value\n        this.#state = state\n        this.#runTask()\n    }\n\n    #runTask() {\n        queueMicrotask(() => {\n            if (this.#state !== PENDING) {\n                this.#handlers.forEach((cb) => cb())\n                this.#handlers = &#91;]\n            }\n        })\n\n    }\n\n    then(onFulfilled, onRejected) {\n        return new MyPromise((resolve, reject) => {\n\n            this.#handlers.push(() => {\n                try {\n                    const cb = this.#state === FULFILLED ? onFulfilled : onRejected\n                    const res = typeof cb === 'function' ? cb(this.#value) : this.#value\n                    if (isPromiseLike(res)) {\n                        res.then(resolve, reject)\n                    }\n                    else {\n                        resolve(res)\n                    }\n\n                } catch (error) {\n                    reject(error)\n                }\n\n            })\n            this.#runTask()\n        })\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1.\u6784\u9020\u5668 2.Promise.then()<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,1,46],"tags":[],"class_list":["post-2039","post","type-post","status-publish","format-standard","hentry","category-javascript","category-1","category-46"],"_links":{"self":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2039"}],"version-history":[{"count":3,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2039\/revisions"}],"predecessor-version":[{"id":2042,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2039\/revisions\/2042"}],"wp:attachment":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}