{"id":535249,"date":"2021-09-01T00:33:57","date_gmt":"2021-08-31T16:33:57","guid":{"rendered":"http:\/\/4563.org\/?p=535249"},"modified":"2021-09-01T00:33:57","modified_gmt":"2021-08-31T16:33:57","slug":"%e7%9c%8b%e5%88%b0%e4%b8%80%e6%ae%b5%e4%bb%a3%e7%a0%81%ef%bc%8c-settimeoutresolve-0-%e4%b8%8d%e6%98%af%e5%be%88%e6%98%8e%e7%99%bd%e7%94%a8%e6%84%8f%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=535249","title":{"rendered":"\u770b\u5230\u4e00\u6bb5\u4ee3\u7801\uff0c setTimeout(resolve, 0), \u4e0d\u662f\u5f88\u660e\u767d\u7528\u610f\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1> \u770b\u5230\u4e00\u6bb5\u4ee3\u7801\uff0c setTimeout(resolve, 0), \u4e0d\u662f\u5f88\u660e\u767d\u7528\u610f\uff1f <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 13<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>const whenStable = async () =&gt;   await act(async () =&gt; {     await new Promise((resolve) =&gt; setTimeout(resolve, 0));   }); <\/code><\/pre>\n<p>\u539f\u5e16\u5730\u5740\uff1a https:\/\/stackoverflow.com\/questions\/60137762\/how-can-i-test-a-react-hooks-component-by-changing-usestate?rq=1 \u53ea\u6709\u4e00\u4e2a\u56de\u590d\uff0c\u56de\u590d\u91cc\u9762\u7684\u6d4b\u8bd5\u6587\u4ef6\uff0c\u91cc\u9762\u7528\u5230\u4e86\u8fd9\u4e2a<\/p>\n<p>\u4e0d\u662f\u5f88\u7406\u89e3\u8fd9\u4e2a\u7684\u7528\u610f\u3002\u5982\u679c\u8bf4\u9700\u8981\u5ef6\u65f6\uff0c\u90a3\u4e3a\u4ec0\u4e48 setTimeout \u7684\u53c2\u6570\u662f 0? \u8fd8\u662f\u8bf4\uff0c\u4f5c\u8005\u53ea\u662f\u4e3e\u4e2a\u4f8b\u5b50\uff1f\u5e94\u8be5\u8bbe\u4e3a\u6bd4\u5982 500\uff0cetc.?<\/p>\n<p>\u4ee5\u53ca\uff0c\u8fd9\u4e2a\u8c8c\u4f3c\u6709 warning<\/p>\n<pre><code>Warning: The callback passed to ReactTestUtils.act(...) function must not return anything. <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>15<\/span>) <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"6690598\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Puteulanus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> setTimeout 0 \u4e0d\u662f\u7cbe\u786e\u7684\u6ca1\u6709\u5ef6\u8fdf\uff0c\u770b\u8fd9\u610f\u601d\u662f\u60f3\u7b49\u5f85\u5230\u4e0b\u4e2a event loop \uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690599\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : just1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u7c7b\u4f3c\u4e8e nextTick <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690600\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : misdake <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> microtask \u548c macrotask \u7684\u533a\u522b\u5427\u3002promise \u662f microtask \u3002setTimeout \u662f macrotask\uff0c\u4f1a\u5728\u6240\u6709 promise \u6267\u884c\u4e4b\u540e\u518d\u6267\u884c\u3002<br \/>\u611f\u89c9\u539f\u56e0\u5e94\u8be5\u662f\uff1a\u754c\u9762\u53d8\u52a8\u90fd\u662f\u901a\u8fc7 microtask \u6765\u5b9e\u73b0\u7684\uff0csetTimeout \u7684\u51fd\u6570\u4e00\u65e6\u8fd0\u884c\u5c31\u8bf4\u660e\u5df2\u5168\u90e8\u6267\u884c\u5b8c\u6bd5\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690601\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : towave <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> https:\/\/blog.towavephone.com\/browser-working-principle-cycle-system\/#webapi%EF%BC%9Asettimeout-%E6%98%AF%E6%80%8E%E4%B9%88%E5%AE%9E%E7%8E%B0%E7%9A%84 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690602\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : RexG <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @just1 @misdake \u8bf4\u7684\u90fd\u5bf9\uff0c\u5c31\u662f\u8fd9\u4e2a\u64cd\u4f5c <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690603\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : eason1874 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u7528\u9014\u662f\u9632\u963b\u585e\u3002<br \/>\u6d4f\u89c8\u5668\u662f\u6309\u987a\u5e8f\u5904\u7406\u4e8b\u4ef6\u7684\uff0csetTimeout \u56de\u8c03\u4e8b\u4ef6\u6392\u5728\u9875\u9762\u666e\u901a JavaScript \u4ee3\u7801\u540e\u9762\uff0c\u6240\u4ee5\u6bd4\u8f83\u5360\u65f6\u95f4\u7684 JavaScript \u53ef\u4ee5\u653e\u5230 setTimeout \u91cc\u9762\uff0c\u7b49\u9875\u9762\u6e32\u67d3\u597d\u518d\u53bb\u6267\u884c\uff0c\u9632\u6b62\u963b\u585e\u5bfc\u81f4\u767d\u5c4f\u4ec0\u4e48\u7684\u3002<br \/>setTimeout \u4e0d\u662f\u771f\u6b63\u7684\u5b9a\u65f6\u4efb\u52a1\uff0c\u610f\u601d\u662f\u6700\u5feb\u5728\u8fd9\u4e2a\u65f6\u95f4\u6267\u884c\uff0c\u6162\u7684\u8bdd\uff0c\u8bf4\u4e0d\u51c6\u3002\u5982\u679c\u524d\u9762\u4e8b\u4ef6\u6709\u963b\u585e\u4e00\u8f88\u5b50\uff0c\u90a3\u4e00\u8f88\u5b50\u4e5f\u4e0d\u4f1a\u6267\u884c\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690604\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KouShuiYu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div>  \u4f30\u8ba1\u662f\u786e\u4fdd DOM \u5df2\u7ecf\u66f4\u65b0 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690605\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : violetlai <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6709\u4e9b\u63d2\u4ef6\u9700\u8981\u7b49 dom \u66f4\u65b0 \u624d\u641e\u8fd9\u4e2a <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690606\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 2i2Re2PLMaDnghL <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6211\u89c9\u5f97\u53d6\u4e2a\u540d\u5b57\u6e05\u695a\u70b9<br \/>const nexttick = () =&gt; new Promise((resolve) =&gt; setTimeout(resolve, 0));<br \/>\/\/&#8230;<br \/>await nexttick()<br \/>\/\/&#8230;<br \/>\u81f3\u4e8e\u4f60\u7684 warning\uff0c\u5219\u662f\u6765\u6e90\u4e8e act(async () =&gt; {}) \u8fd9\u90e8\u5206\u3002\u4f46\u7406\u8bba\u4e0a\u6765\u8bf4\u8fd9\u6bb5\u4e2d\u786e\u5b9e\u4e0d\u5305\u542b\u4efb\u4f55 return\uff0c\u662f\u4e0d\u662f\u8fd4\u56de\u4e86\u4e00\u4e2a Promise&lt;undefined&gt; \u7684\u539f\u56e0\uff1f <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690607\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wanguorui123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6dfb\u52a0\u5230\u4e8b\u4ef6\u961f\u5217\u672b\u5c3e\u4e0b\u4e2a\u5468\u671f\u8c03\u7528 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690608\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : slime7 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u6211\u53bb\u6284 vuetify \u7684\u6c34\u6ce2\u7eb9\u4ee3\u7801\u65f6\u4e5f\u89c1\u8fc7\u8fd9\u4e2a\u7528\u6cd5\uff0c\u7528\u5728\u6dfb\u52a0\u6c34\u6ce2\u7eb9\u52a8\u753b\u7684\u8fdb\u5165 class \u4e0a\uff0c\u6211\u5f53\u65f6\u731c\u6d4b\u662f\u9700\u8981\u5148\u8bbe\u7f6e\u4e00\u4e2a\u521d\u59cb\u6837\u5f0f\uff0c\u518d\u7528\u8fd9\u4e2a\u65b9\u5f0f\u8bbe\u7f6e\u9700\u8981\u53d8\u5f62\u5230\u7684\u6837\u5f0f\uff0c\u8fd9\u6837\u624d\u80fd\u4ea7\u751f css \u7684 transition <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690609\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Taikyo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u52a0\u5165\u5b8f\u4efb\u52a1\uff0c\u53ea\u662f\u4e3a\u4e86\u4e0d\u8ba9\u5b83\u88ab\u7acb\u523b\u6267\u884c\uff0c\u53ef\u4ee5\u5728\u4e0b\u4e00\u4e2a event loop \u53bb\u6267\u884c\u3002 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690610\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xxz0315 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> \u76f8\u5f53\u4e0b\u4e00\u5e27\u7684\u610f\u601d <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690611\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @2i2Re2PLMaDnghL <br \/>\u6709\u6ca1\u6709\u529e\u6cd5\u4fee\u6539\u4e00\u4e0b\uff0c\u6d88\u9664\u8fd9\u4e2a\u8b66\u544a\uff1f\u5c31\u6309\u5f53\u524d\u7684\u7528\u9014 <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"6690612\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 2i2Re2PLMaDnghL <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>  <\/span> <\/div>\n<\/p><\/div>\n<div> @yazoox \u7b80\u5355\u5730<br \/>act(async()=&gt;{&#8230;})<br \/>\u8f6c\u5316\u4e3a<br \/>act(()=&gt;{(async()=&gt;{&#8230;})()})<br \/>\u4f46\u5176\u5b9e\u6ca1\u6709\u4efb\u4f55\u610f\u4e49\uff0c\u662f\u8fd9\u4e2a warning \u672c\u8eab\u6709\u95ee\u9898\u3002<br \/>\u6211\u731c\u60f3\u8fd9\u4e2a warning \u7684\u8bbe\u7f6e\u4f3c\u4e4e\u662f\u4e3a\u4e86\u907f\u514d\u6709\u4eba\u300c\u60f3\u5f53\u7136\u5730\u300d\u7528 act callback \u4f20\u9012\u6d4b\u8bd5\u5931\u8d25\u4fe1\u53f7\u3002<br \/>\u4e3a\u4e86\u7ed5\u8fc7\u4e00\u4e2a\u8fc7\u4e8e\u5bbd\u6cdb\u7684 warning \u8fd9\u4e48\u505a\u4e0d\u597d\uff0c\u4f60\u5e94\u5f53\u4fee\u6b63 warning \u7684\u63d0\u4f9b\u8005\u3002<br \/>\u770b\u4e0a\u53bb\u8fd9\u4e2a warning \u4e0d\u662f react \u63d0\u4f9b\u7684\uff0c\u5728 react \u4ee3\u7801\u4e2d\u5168\u6587\u641c\u7d22 &#8220;must not return anything&#8221; \u7c7b\u4f3c\u6587\u5b57\u5747\u5728\u5904\u7406\u8981\u6c42 effect \u53ea\u80fd\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002  <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u770b\u5230\u4e00\u6bb5\u4ee3\u7801\uff0c setTimeou&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/535249"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535249"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/535249\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535249"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}