{"id":262,"date":"2022-04-11T09:19:22","date_gmt":"2022-04-11T09:19:22","guid":{"rendered":"http:\/\/114.132.224.28\/?p=262"},"modified":"2022-04-11T09:19:22","modified_gmt":"2022-04-11T09:19:22","slug":"%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0promise","status":"publish","type":"post","link":"http:\/\/localhost\/2022\/04\/11\/%e4%b8%80%e6%ad%a5%e6%ad%a5%e5%ae%9e%e7%8e%b0promise\/","title":{"rendered":"\u4e00\u6b65\u6b65\u5b9e\u73b0promise"},"content":{"rendered":"\n
promise\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\uff1a<\/p>\n\n\n\n
\u524d\u7f6e\u77e5\u8bc6\uff1a<\/p>\n\n\n\n
\u5148\u770b\u770bnew Promise\u63a5\u6536\u4ec0\u4e48\u53c2\u6570\uff0c\u63a5\u53d7\u4e00\u4e2aexecutor\u51fd\u6570\uff0c\u8fd9\u4e2aexecutor\u4f1a\u63a5\u53d7\u4e24\u4e2a\u51fd\u6570\u53c2\u6570\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u7684\u8c03\u7528\u4f1a\u5f71\u54cdPromise\u5185\u90e8\u7684\u72b6\u6001<\/p>\n\n\n\n
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown><\/code><\/pre>\n\n\n\n\u518d\u770b\u770bPromise.then(),\u63a5\u53d7\u4e24\u4e2a\u51fd\u6570\u53c2\u6570\uff0c\u4e00\u4e2a\u8d1f\u8d23\u6210\u529f\u7684\u56de\u8c03\u4e00\u4e2a\u8d1f\u8d23\u9519\u8bef\u7684\u56de\u8c03<\/p>\n\n\n\n
Promise<unknown>.then<unknown, never>(onfulfilled?: ((value: unknown) => unknown) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<unknown>\n<\/code><\/pre>\n\n\n\n\u90a3\u6211\u4eec\u5148\u4ece\u4e00\u822c\u56de\u8c03\u5f00\u59cb\uff0c\u56de\u8c03\u662f\u4e00\u79cd\u63a7\u5236\u53cd\u8f6c\uff0c\u51fd\u6570\u5982\u4f55\u8c03\u7528\u4ec0\u4e48\u65f6\u5019\u8c03\u7528\u5c06\u7531\u5185\u90e8\u51fd\u6570\u51b3\u5b9a<\/p>\n\n\n\n
let result\nfunction normalCb(cb) {\n cb(result)\n}\nresult = 'success'\nnormalCb(console.log)\n<\/code><\/pre>\n\n\n\n\u4f46\u662f\u6211\u4eec\u8fd9\u91cc\u53ea\u80fd\u76d1\u542c\u5230\u6210\u529f\uff0c\u9519\u8bef\u7684\u56de\u8c03\u4e5f\u9700\u8981\u5904\u7406<\/p>\n\n\n\n
let result\nlet error\nconst PENDING = 0;\nconst FULFILLED = 1;\nconst REJECTED = 2;\nlet state = PENDING\nfunction normalErrorCb(cb, errorCb) {\n switch(state) {\n case FULFILLED: {\n return cb(result)\n }\n case REJECTED: {\n return errorCb(error)\n }\n default: {\n console.log('pending...')\n }\n }\n}\nfunction setStatus(status, param) {\n switch(status) {\n case FULFILLED: {\n state = FULFILLED\n result = param\n break\n }\n case REJECTED: {\n state = REJECTED\n error = param\n break\n }\n default: {\n console.log('pending...')\n }\n }\n}\n\nsetStatus(FULFILLED, 'success')\nnormalErrorCb(console.log, console.error)<\/code><\/pre>\n\n\n\n\u8fd9\u6837\u6211\u4eec\u65e2\u80fd\u76d1\u542c\u5230\u6210\u529f\u548c\u9519\u8bef\uff0c\u4f46\u5176\u5b9e\u72b6\u6001\u53ea\u5e94\u8be5\u6539\u53d8\u4e00\u6b21\uff0c\u53ea\u4f1a\u8c03\u7528\u6210\u529f\u6216\u9519\u8bef\uff0c\u800c\u4e14\u53ef\u4ee5\u591a\u6b21\u76d1\u542c\u51fd\u6570<\/p>\n\n\n\n
\/\/ \u72b6\u6001\u53ea\u80fd\u6539\u53d8\u4e00\u6b21\uff0c\u5f02\u6b65\u8c03\u7528\uff0c\u5e76\u80fd\u63a5\u53d7\u591a\u4e2a\u56de\u8c03\nlet result\nlet error\nconst PENDING = 0\nconst FULFILLED = 1\nconst REJECTED = 2\nlet state = PENDING\nlet done = false\nconst handlers = []\nfunction normalErrorCb (cb, errorCb) {\n setTimeout(() => {\n switch (state) {\n case FULFILLED: {\n return cb(result)\n }\n case REJECTED: {\n return errorCb(error)\n }\n case PENDING: {\n return handlers.push({ cb, errorCb })\n }\n default: {\n console.log('wrong status')\n }\n }\n }, 0)\n}\n\nfunction setStatus (status, param) {\n if (done) return\n switch (status) {\n case FULFILLED: {\n done = true\n state = FULFILLED\n result = param\n handlers.forEach((handler) => handler.cb(result))\n break\n }\n case REJECTED: {\n done = true\n state = REJECTED\n error = param\n handlers.forEach((handler) => handler.errorCb(error))\n break\n }\n default: {\n console.log('pending...')\n }\n }\n}\n\nnormalErrorCb(console.log, console.error)\nsetStatus(FULFILLED, 'success')\nsetStatus(REJECTED, 'error')\nnormalErrorCb(console.log.bind(undefined, 'one more'), console.error)<\/code><\/pre>\n\n\n\n\u73b0\u5728\u6211\u4eec\u7684\u51fd\u6570\u72b6\u6001\u53ea\u4f1a\u6539\u53d8\u4e00\u6b21\uff0c\u800c\u4e14\u80fd\u591a\u6b21\u76d1\u542c\uff0c\u4f46\u662f\u73b0\u5728\u5168\u5c40\u72b6\u6001\u90fd\u662f\u53d8\u91cf\uff0c\u6211\u4eec\u8981\u628a\u4ed6\u7528\u51fd\u6570\u5305\u8d77\u6765<\/p>\n\n\n\n
\/\/ \u6211\u4eec\u58f0\u660e\u4e86\u5f88\u591a\u4e1c\u897f\u5728\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u6211\u4eec\u5e94\u8be5\u628a\u4ed6\u5305\u8d77\u6765\nfunction MockPromise (fn) {\n let result\n let error\n const PENDING = 0\n const FULFILLED = 1\n const REJECTED = 2\n let state = PENDING\n let done = false\n const handlers = []\n this.then = function normalErrorCb (cb, errorCb) {\n setTimeout(() => {\n switch (state) {\n case FULFILLED: {\n return cb(result)\n }\n case REJECTED: {\n return errorCb(error)\n }\n case PENDING: {\n return handlers.push({ cb, errorCb })\n }\n default: {\n console.log('wrong status')\n }\n }\n }, 0)\n }\n\n function setStatus (status, param) {\n if (done) return\n switch (status) {\n case FULFILLED: {\n done = true\n state = FULFILLED\n result = param\n handlers.forEach((handler) => handler.cb(result))\n break\n }\n case REJECTED: {\n done = true\n state = REJECTED\n error = param\n handlers.forEach((handler) => handler.errorCb(error))\n break\n }\n default: {\n console.log('pending...')\n }\n }\n }\n\n fn(resolve, reject)\n\n function resolve (successValue) {\n setStatus(FULFILLED, successValue)\n }\n function reject (error) {\n setStatus(REJECTED, error)\n }\n \/\/ \u600e\u4e48\u66b4\u9732\u8fd9\u4e2a\u65b9\u6cd5\u5462\uff1f\u4e00\u65b9\u9762\u53ef\u4ee5\u50cfthen\u4e00\u6837\u76f4\u63a5\u66b4\u9732\uff0c\u4f46\u662f\u66f4\u597d\u7684\u65b9\u6cd5\u662f\uff0c\u901a\u8fc7\u6784\u9020\u51fd\u6570\u7684\u65f6\u5019\u66b4\u9732\u51fa\u53bb\uff0c\u8fd9\u6837resolve,reject\u4e0d\u4f1a\u88ab\u5916\u754c\u6539\u53d8\n \/\/ this.resolve = function (successValue) {\n \/\/ setStatus(FULFILLED, successValue)\n \/\/ }\n \/\/ this.reject = function (error) {\n \/\/ setStatus(REJECTED, error)\n \/\/ }\n}\n\nconst p = new MockPromise((resolve, reject) => {\n setTimeout(() => {\n resolve('success')\n reject('error')\n }, 500)\n})\np.then(console.log, console.error)<\/code><\/pre>\n\n\n\n\u73b0\u5728\u8ddf\u6211\u4eec\u7684promise\u5df2\u7ecf\u5f88\u50cf\u4e86\uff0c\u4f46\u662f\u65e0\u6cd5\u505a\u5230\u94fe\u5f0f\u8c03\u7528,\u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\u7684\u65f6\u5019\u8fd8\u9700\u8981\u8003\u8651\uff0c\u5982\u679c\u518dthen\u91cc\u9762\u4e5f\u8c03\u7528\u4e86\u4e00\u4e2apromise,\u90a3\u8fd9\u4e2a\u65b0\u8fd4\u56de\u7684promise\u5e94\u8be5\u662f\u52a0\u5728\u8fd9\u4e2apromise\u7684\u56de\u8c03\uff0c\u53ea\u6709\u8fd9\u4e2apromise\u6210\u529f\u6267\u884c\u540e\uff0c\u65b0\u7684promise\u624d\u51b3\u8bae\u72b6\u6001\uff0c\u6240\u4ee5resolve\u8fd8\u6709\u53ef\u80fd\u4f1a\u5f97\u5230\u4e00\u4e2apromise<\/p>\n\n\n\n
\/\/ \u5b9e\u73b0\u94fe\u5f0f\u8c03\u7528\nfunction MockPromise (fn) {\n let result\n let error\n const PENDING = 0\n const FULFILLED = 1\n const REJECTED = 2\n let state = PENDING\n let done = false\n const handlers = []\n this.then = function normalErrorCb (cb, errorCb) {\n return new MockPromise((resolve, reject) => {\n setTimeout(() => {\n switch (state) {\n case FULFILLED: {\n const cbValue = cb(result) \/\/ \u8fd9\u91cc\u7684cb\u8fd4\u56de\u503c\u8fd8\u662fpromise\u600e\u4e48\u529e\uff1f\u8fd9\u91cc\u7684\u7684resolve\u5e94\u8be5\u662f\u7b49\u8fd4\u56de\u7684promise\u7ed3\u675f\u540e\n return resolve(cbValue)\n }\n case REJECTED: {\n const errorValue = errorCb(error)\n reject(errorValue)\n return errorValue\n }\n case PENDING: {\n return handlers.push({ cb, errorCb })\n }\n default: {\n console.log('wrong status')\n }\n }\n }, 0)\n })\n }\n\n function setStatus (status, param) {\n if (done) return\n switch (status) {\n case FULFILLED: {\n done = true\n state = FULFILLED\n result = param\n handlers.forEach((handler) => handler.cb(result))\n break\n }\n case REJECTED: {\n done = true\n state = REJECTED\n error = param\n handlers.forEach((handler) => handler.errorCb(error))\n break\n }\n default: {\n console.log('pending...')\n }\n }\n }\n\n fn(resolve, reject)\n\n \/\/ function resolve (successValue) { \/\/ \u9ed8\u8ba4\u662f\u666e\u901a\u503c\u76f4\u63a5\u4f20\u9012\u7ed9cb\u4e86\n \/\/ setStatus(FULFILLED, successValue)\n \/\/ }\n function resolve (successValue) {\n const t = typeof successValue\n if (t === 'object' || t === 'function') {\n const then = successValue.then\n if (typeof then === 'function') {\n then((result) => {\n resolve(result)\n }, (error) => {\n reject(error)\n })\n }\n } else {\n setStatus(FULFILLED, successValue)\n }\n }\n function reject (error) {\n setStatus(REJECTED, error)\n }\n}\n\nconst p = new MockPromise((resolve, reject) => {\n resolve('success')\n reject('error')\n})\np.then((result) => {\n console.log(result)\n return new MockPromise((resolve, reject) => {\n resolve('otherSuccess')\n })\n}, console.error).then(console.log, console.error)<\/code><\/pre>\n\n\n\n\u6211\u4eec\u57fa\u672c\u5b9e\u73b0\u4e86\u8fd9\u4e2apromise\uff0c\u5f53\u7136\u8fd8\u6709\u4e00\u4e9b\u53ef\u4ee5\u4f18\u5316\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u518d\u62bd\u8c61\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u770bhttps:\/\/www.promisejs.org\/implementing\/<\/a><\/p>\n\n\n\nreference\uff1a<\/p>\n\n\n\n
- https:\/\/www.promisejs.org\/implementing\/[Implementing promise]<\/li><\/ul>\n\n\n\n
- https:\/\/github.com\/cyanxxx\/blogcode\/tree\/master\/how_to_realize_promise[\u672c\u6587\u6240\u7528\u7684\u4ee3\u7801]<\/li><\/ul>\n\n\n\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
promise\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898\uff1a \u6210\u529f\u7684\u56de\u8c03\u548c\u5931\u8d25\u7684\u56de …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/262"}],"collection":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":0,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}