{"id":3420,"date":"2024-11-10T15:48:32","date_gmt":"2024-11-10T07:48:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3420\/"},"modified":"2024-11-10T15:48:32","modified_gmt":"2024-11-10T07:48:32","slug":"javascript%e4%b8%ad%e4%bd%bf%e7%94%a8promise%e8%a7%a3%e5%86%b3%e5%bc%82%e6%ad%a5%e5%8a%a0%e8%bd%bd%ef%bc%88%e5%9b%be%e7%89%87%e3%80%81css%e3%80%81js%e7%ad%89%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3420\/","title":{"rendered":"Javascript\u4e2d\u4f7f\u7528Promise\u89e3\u51b3\u5f02\u6b65\u52a0\u8f7d\uff08\u56fe\u7247\u3001css\u3001js\u7b49\uff09"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173103841919458.jpg\" class=\"aligncenter\" title=\"Javascript\u4e2d\u4f7f\u7528Promise\u89e3\u51b3\u5f02\u6b65\u52a0\u8f7d\uff08\u56fe\u7247\u3001css\u3001js\u7b49\uff09\u63d2\u56fe\" alt=\"Javascript\u4e2d\u4f7f\u7528Promise\u89e3\u51b3\u5f02\u6b65\u52a0\u8f7d\uff08\u56fe\u7247\u3001css\u3001js\u7b49\uff09\u63d2\u56fe\" \/><\/p>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u52a0\u8f7d\u9759\u6001\u8d44\u6e90\u540e\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\u3002\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u4ea7\u751f\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u590d\u6742\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528promise\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h3> \u793a\u4f8b\uff08\u52a0\u8f7d\u56fe\u50cf\uff09 <\/h3>\n<pre>function loadimg(imgsrc) {\n  return new promise(function(resolve, reject){\n    img.load = () =&gt; {\n      \/\/ asynchronous code here\n      resolve()\n    }\n\n    img.onerror = () =&gt; {\n      reject()\n    }\n  })\n}\n\n\nloadimg('src.jpg').then(()=&gt;{\n  \/\/ operations after loading image here\n}).catch(()=&gt;{\n \/\/ error handling\n})\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u52a0\u8f7d\u591a\u5f20\u56fe\u50cf\u7684\u793a\u4f8b <\/h3>\n<pre>function loadImg(imgSrc) {\n  return new Promise(function(resolve, reject){\n    img.load = () =&gt; {\n      \/\/ asynchronous code here\n      resolve()\n    }\n\n    img.onError = () =&gt; {\n      reject()\n    }\n  })\n}\n\nconst imgList = ['1.jpg', '2.jpg', '3.jpg', '4.jpg']\nlet imgLoadingPromise = []\nfor(let img of imgList) imgLoadingPromise.push(loadImg(img))\n\nPromise.all(imgLoadingPromise).then(()=&gt;{\n  \/\/ operations after loading image here\n}).catch(()=&gt;{\n \/\/ error handling\n})\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>promise.all() \u63a5\u6536\u4e00\u7ec4 promise\uff0c\u53ea\u6709\u5f53\u6240\u6709 promise \u90fd\u53d8\u4e3a fullfilled \u72b6\u6001\u65f6\uff0c\u8fd9\u4e9b promise \u624d\u4f1a\u53d8\u4e3a fullfilled \u72b6\u6001<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavascript\u4e2d\u4f7f\u7528Promise\u89e3\u51b3\uff08\u56fe\u7247\u3001\u3001js\u7b49\uff09\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>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u52a0\u8f7d\u9759\u6001\u8d44\u6e90\u540e\u5b8c\u6210\u4e00\u4e9b\u64cd\u4f5c\u3002\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u4ea7\u751f\u591a\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u590d\u6742\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528promise\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002 \u793a\u4f8b\uff08\u52a0\u8f7d\u56fe\u50cf\uff09 function loadimg(imgsrc) { return new promise(function(resolve, reject){ img.load = () =&gt; { \/\/ asynchronous code here resolve() } img.onerror = () =&gt; { reject() } }) } loadimg(&#8216;src.jpg&#8217;).then(()=&gt;{ \/\/ operations after loading image here }).catch(()=&gt;{ \/\/ error handling }) \u767b\u5f55\u540e\u590d\u5236 \u52a0\u8f7d\u591a\u5f20\u56fe\u50cf\u7684\u793a\u4f8b function loadImg(imgSrc) { return new Promise(function(resolve, reject){ img.load = () =&gt; { [&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-3420","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3420","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=3420"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3420\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}