{"id":2243,"date":"2024-11-10T10:04:06","date_gmt":"2024-11-10T02:04:06","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2243\/"},"modified":"2024-11-10T10:04:06","modified_gmt":"2024-11-10T02:04:06","slug":"%e5%9b%9e%e8%b0%83-javascript-%e6%8c%91%e6%88%98","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2243\/","title":{"rendered":"\u56de\u8c03 &#8211; JavaScript \u6311\u6218"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173069825314003.jpg\" class=\"aligncenter\" title=\"\u56de\u8c03 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" alt=\"\u56de\u8c03 &#8211; JavaScript \u6311\u6218\u63d2\u56fe\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 hub \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n<hr>\n<h2> \u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u76f8\u5173\u7684\u6311\u6218 <\/h2>\n<hr>\n<h3> \u5728\u7279\u5b9a\u79d2\u540e\u8c03\u7528\u56de\u8c03 <\/h3>\n<pre>\/**\n * @param {function} callbackfn\n * @param {delay} number\n * @return {object}\n *\/\n\nfunction invokelater(callbackfn, delay) {\n  const timerid = settimeout(() =&gt; {\n    callbackfn(null, 'run');\n  }, delay);\n\n  return {\n    clear: () =&gt; cleartimeout(timerid),\n  }\n}\n\n\/\/ usage example\nconst cancel = invokelater((err, data) =&gt; {\n  console.log(data);\n  cancel.clear();\n}, 2000);\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u538b\u6241\u91cd\u51fb <\/h3>\n<pre>\/**\n * @param {function} fn\n * @return {function}\n *\/\n\nfunction flattenThunk(fn) {\n  return function (callbackFn) {\n    function resolveThunk(err, result) {\n      if (err) {\n        callbackFn(err, undefined);\n        return;\n      }\n\n      if (typeof result === 'function') {\n        result(resolveThunk);\n      } else {\n        callbackFn(undefined, result);\n      }\n    }\n\n    fn(resolveThunk);\n  }\n}\n\n\/\/ Usage example\nfunction fn1(callbackFn) {\n  setTimeout(() =&gt; {\n    callbackFn(null, 'ok');\n  }, 10);\n}\n\nfunction fn2(callbackFn) {\n  setTimeout(() =&gt; {\n    callbackFn(null, fn1);\n  }, 10);\n}\n\nfunction fn3(callbackFn) {\n  setTimeout(() =&gt; {\n    callbackFn(null, fn2);\n  }, 10);\n}\n\nflattenThunk(fn3)((err, data) =&gt; {\n  console.log(data); \/\/ 'ok'\n});\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h2> \u53c2\u8003 <\/h2>\n<ul>\n<li>\u56de\u8c03\uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\uff09- wikipedia.org<\/li>\n<li>\u56de\u8c03\u51fd\u6570 &#8211; mdn<\/li>\n<li>thunk &#8211; wikipedia.org<\/li>\n<li>54\u3002\u538b\u5e73 thunk &#8211; bfe.dev<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u56de\u8c03 &#8211; JavaScript \u6311\u6218\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60a8\u53ef\u4ee5\u5728 hub \u4ed3\u5e93\u4e2d\u627e\u5230\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u7684\u6240\u6709\u4ee3\u7801\u3002 \u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u76f8\u5173\u7684\u6311\u6218 \u5728\u7279\u5b9a\u79d2\u540e\u8c03\u7528\u56de\u8c03 \/** * @param {function} callbackfn * @param {delay} number * @return {object} *\/ function invokelater(callbackfn, delay) { const timerid = settimeout(() =&gt; { callbackfn(null, &#8216;run&#8217;); }, delay); return { clear: () =&gt; cleartimeout(timerid), } } \/\/ usage example const cancel = invokelater((err, data) =&gt; { console.log(data); cancel.clear(); }, 2000); \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2243","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2243"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2243\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}