{"id":215,"date":"2021-02-10T11:30:27","date_gmt":"2021-02-10T03:30:27","guid":{"rendered":"http:\/\/gjweb.top\/?p=215"},"modified":"2021-02-10T11:30:28","modified_gmt":"2021-02-10T03:30:28","slug":"async-await-%e5%85%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=215","title":{"rendered":"async await \u5168\u89e3"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>promise\u7684API<\/li><li>promise\u7684\u4f7f\u7528\u573a\u666f<\/li><li>async\/await<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">promise\u7684API<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u89c4\u7528\u6cd5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  let text = new Promise((resolve,reject)=&gt;{<br> &nbsp; &nbsp; &nbsp;  resolve(\"\u6210\u529f\u65f6\u8fd4\u56de\")<br> &nbsp; &nbsp; &nbsp;  reject(\"\u5931\u8d25\u65f6\u8fd4\u56de\")<br> &nbsp;  })<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Promise.resolve(resolve)<\/h3>\n\n\n\n<p>\u5236\u9020\u4e00\u4e2a\u6210\u529f(\u6216\u5931\u8d25)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Promise.reject(reject)<\/h3>\n\n\n\n<p>\u5236\u9020\u4e00\u4e2a\u5931\u8d25<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">promise.all(\u6570\u7ec4)<\/h3>\n\n\n\n<p>\u7b49\u5f85\u5168\u90e8\u6210\u529f,\u6216\u8005\u6709\u4e00\u4e2a\u5931\u8d25<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">promise.race(\u6570\u7ec4)<\/h3>\n\n\n\n<p>\u7b49\u5f85\u7b2c\u51e0\u4e2a\u72b6\u6001\u6539\u53d8<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">promise.allSettled(\u6570\u7ec4)<\/h3>\n\n\n\n<p>\u7b49\u5f85\u5168\u90e8\u72b6\u6001\u6539\u53d8,\u76ee\u524d\u5904\u4e8estage-4(\u5e76\u672a\u89c4\u8303) \u624b\u5199allSettled<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  let ppromise1 =()=&gt;{return  new Promise((resolve,reject)=&gt;{<br> &nbsp; &nbsp; &nbsp;  setTimeout(()=&gt;{reject(\"\u7b2c\u4e00\u6247\u95e8\u5173\u4e86\")},3000)<br> &nbsp;  })}<br>\u200b<br> &nbsp;  let ppromise2 =()=&gt;{ return new Promise((resolve,reject)=&gt;{<br> &nbsp; &nbsp; &nbsp;  setTimeout(()=&gt;{reject(\"\u7b2c\u4e8c\u6247\u95e8\u5173\u4e86\")},4000)<br> &nbsp;  })}<br>\u200b<br> &nbsp;  let ppromise3 =()=&gt;{ return new Promise((resolve,reject)=&gt;{<br> &nbsp; &nbsp; &nbsp;  setTimeout(()=&gt;{resolve(\"\u7b2c\u4e09\u6247\u95e8\u5f00\u4e86\")},5000)<br> &nbsp;  })}<br>\u200b<br> &nbsp;  \/\/ Promise.all([ppromise1(),ppromise2(),ppromise3()]).then(null,(reason)=&gt;{console.log(reason)}) \/\/\u8fd4\u56de\u5931\u8d25\u7684\u5bf9\u8c61\u4f1a\u7ec8\u6b62<br> &nbsp;  \/\/\u5197\u4f59\u7684\u4ee3\u7801<br> &nbsp;  \/\/ Promise.all([ppromise1().then(()=&gt;({status:'ok'}),()=&gt;({status:\"not ok\"})),<br> &nbsp;  \/\/ ppromise2().then(()=&gt;({status:'ok'}),()=&gt;({status:\"not ok\"})),<br> &nbsp;  \/\/ ppromise3().then(()=&gt;({status:'ok'}),()=&gt;({status:\"not ok\"}))]).then((result)=&gt;{console.log(result)})<br> &nbsp;  \/\/ console.log(\"12.\")<br> &nbsp;  \/\/\u4e0a\u9762\u7684\u4f18\u5316<br> &nbsp;  let x = (paomise)=&gt;{return paomise.then(()=&gt;({status:'ok'}),()=&gt;({status:\"not ok\"}))};<br> &nbsp;  Promise.all([x(ppromise1()),x(ppromise2()),x(ppromise3())]).then((reason)=&gt;{console.log(reason)})<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">promise\u7684\u4f7f\u7528\u573a\u666f<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u591a\u6b21\u5904\u7406\u4e00\u4e2a\u7ed3\u679c\u65f6<\/li><li>\u4e32\u884c<ol><li>\u628a\u4efb\u52a1\u653e\u8fdb\u961f\u5217\u4e2d,\u5b8c\u6210\u4e00\u4e2a\u5728\u505a\u4e0b\u4e00\u4e2a<\/li><\/ol><\/li><li>\u5e76\u884c<ol><li>Promise.all([p1,p2])<\/li><li>promise.allSettled<\/li><li>\u624b\u5199allSettled<\/li><\/ol><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">async\/await<\/h2>\n\n\n\n<p>\u57fa\u672c\u7528\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  const fn = async ()=&gt;{<br> &nbsp; &nbsp; &nbsp;  const temp = await makePromise()<br> &nbsp; &nbsp; &nbsp;  return temp + 1<br> &nbsp;  }<\/pre>\n\n\n\n<p>await \u9519\u8bef\u5904\u7406<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  const response = await axios.get('.\/xxx').then(null,errorHandler)<br> &nbsp;  console.log(response)<\/pre>\n\n\n\n<p>await\u7684\u4f20\u67d3\u6027<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  console.log(1)<br> &nbsp;  await console.log(2)<br> &nbsp;  console.log(3)<\/pre>\n\n\n\n<p>\u6b64\u65f6\u7684consoel.log(3)\u53d8\u6210\u4e86\u5f02\u6b65\u4efb\u52a1<\/p>\n\n\n\n<p>await\u7684\u5929\u7136\u4e32\u884c,\u5728for\u91cc\u9762\u662f\u4e32\u884c;\u5728froEach\u91cc\u9762\u662f\u5e76\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>promise\u7684API promise\u7684\u4f7f\u7528\u573a\u666f async\/await promise\u7684API \u5e38\u89c4\u7528\u6cd5 &#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-js"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}