{"id":291715,"date":"2019-07-05T21:00:33","date_gmt":"2019-07-05T21:00:33","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=291715"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=291715","title":{"rendered":"\u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c Async\/Await \u0432 JavaScript \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/2c8\/f4a\/252\/2c8f4a252d33ee663665fd7df02b45ed.jpg\"> <\/p>\n<p>  \u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 Async\/Await \u0432 JavaScript. \u0412 \u0446\u0435\u043b\u043e\u043c, Async\/Await \u2014 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0414\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0438\u0441\u0430\u043b\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432 \u0438 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432. \u0410\u0432\u0442\u043e\u0440 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 Async\/Await, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<blockquote><p><b>\u041d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c:<\/b> <i>\u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u00ab\u0425\u0430\u0431\u0440\u0430\u00bb \u2014 \u0441\u043a\u0438\u0434\u043a\u0430 10 000 \u0440\u0443\u0431\u043b\u0435\u0439 \u043f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u043a\u0443\u0440\u0441 Skillbox \u043f\u043e \u043f\u0440\u043e\u043c\u043e\u043a\u043e\u0434\u0443 \u00ab\u0425\u0430\u0431\u0440\u00bb.<\/i><\/p>\n<p>  <b>Skillbox \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442:<\/b> \u041e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441 <a href=\"https:\/\/skillbox.ru\/java\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=JAVDEV&amp;utm_content=articles&amp;utm_term=async\">\u00abJava-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u00bb<\/a>.<\/p><\/blockquote>\n<p>  <\/p>\n<h3>Callback <\/h3>\n<p>  Callback \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0432\u044b\u0437\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u0442\u043b\u043e\u0436\u0435\u043d \u043d\u0430 \u043d\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u0420\u0430\u043d\u044c\u0448\u0435 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u0432 \u0442\u0435\u0445 \u0443\u0447\u0430\u0441\u0442\u043a\u0430\u0445 \u043a\u043e\u0434\u0430, \u0433\u0434\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0435 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d \u0441\u0440\u0430\u0437\u0443.<\/p>\n<p>  \u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0447\u0442\u0435\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430 \u043d\u0430 Node.js:<\/p>\n<pre><code class=\"javascript\">fs.readFile(__filename, 'utf-8', (err, data) =&gt; {   if (err) {     throw err;   }   console.log(data); });<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0435\u0431\u0435 \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439: \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0411\u0414 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f Arfat, \u043d\u0443\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u043b\u0435 profile_img_url \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 someserver.com.<br \/>  \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u0438\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 PNG \u0432 JPEG. \u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e, \u043d\u0430 \u043f\u043e\u0447\u0442\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0438\u0441\u044c\u043c\u043e. \u0414\u0430\u043b\u0435\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0432 \u0444\u0430\u0439\u043b transformations.log \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0434\u0430\u0442\u044b.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/ad5\/c3f\/e3b\/ad5c3fe3bf0f7a68a4d3444614c3133e.png\"><\/p>\n<p>  \u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043d\u0430\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e }) \u0432 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430. \u042d\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f Callback Hell \u0438\u043b\u0438 Pyramid of Doom.<\/p>\n<p>  \u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b:<\/p>\n<ul>\n<li>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0441\u043b\u043e\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c.<\/li>\n<li>\u0412 \u043d\u0435\u043c \u0442\u0430\u043a\u0436\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438, \u0447\u0442\u043e \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0443\u0445\u0443\u0434\u0448\u0435\u043d\u0438\u044e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u0434\u0430.<\/li>\n<\/ul>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u0432 JavaScript \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u043f\u0440\u043e\u043c\u0438\u0441\u044b. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432 \u0441\u043b\u043e\u0432\u043e\u043c .then.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/9e1\/503\/0ad\/9e15030ad0dbb431a63d9f98ec228111.png\"><\/p>\n<p>  \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432 \u0441\u0442\u0430\u043b\u043e \u0442\u043e, \u0447\u0442\u043e \u0441 \u043d\u0438\u043c\u0438 \u043a\u043e\u0434 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0443\u0447\u0448\u0435, \u043f\u0440\u0438\u0447\u0435\u043c \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u0430 \u043d\u0435 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0443 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432 \u0442\u043e\u0436\u0435 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b:<\/p>\n<ul>\n<li>\u041d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e .then.<\/li>\n<li>\u0412\u043c\u0435\u0441\u0442\u043e try\/catch \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f .catch \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435\u0445 \u043e\u0448\u0438\u0431\u043e\u043a.<\/li>\n<li>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u043e\u043c\u0438\u0441\u0430\u043c\u0438 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0434\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430 \u0434\u0430\u043b\u0435\u043a\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0443\u0434\u043e\u0431\u043d\u0430, \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043e\u043d\u0438 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u044e\u0442 \u043a\u043e\u0434.<\/li>\n<\/ul>\n<p>  \u0412\u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043a\u0430\u0436\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u0443\u043d\u043a\u0442\u0430.<\/p>\n<p>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0446\u0438\u043a\u043b for, \u0432\u044b\u0432\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0447\u0438\u0441\u0435\u043b \u043e\u0442 0 \u0434\u043e 10 \u0441\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u043c (0\u2013n \u0441\u0435\u043a\u0443\u043d\u0434). \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u043e\u043c\u0438\u0441\u044b, \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0446\u0438\u043a\u043b \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0447\u0438\u0441\u043b\u0430 \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0442 0 \u0434\u043e 10. \u0422\u0430\u043a, \u0435\u0441\u043b\u0438 \u0432\u044b\u0432\u043e\u0434 \u043d\u0443\u043b\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 6 \u0441\u0435\u043a\u0443\u043d\u0434, \u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u044b \u2014 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432\u044b\u0432\u0435\u0434\u0435\u043d \u043d\u043e\u043b\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c \u0443\u0436\u0435 \u043d\u0430\u0447\u043d\u0435\u0442\u0441\u044f \u043e\u0442\u0441\u0447\u0435\u0442 \u0432\u044b\u0432\u043e\u0434\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u044b.<\/p>\n<p>  \u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c Async\/Await \u043b\u0438\u0431\u043e .sort. \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u2014 \u0432 \u043a\u043e\u043d\u0446\u0435.<\/p>\n<h3>Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h3>\n<p>   \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 async-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 ES2017 (ES8) \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b\u043e \u0437\u0430\u0434\u0430\u0447\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043f\u0440\u043e\u043c\u0438\u0441\u0430\u043c\u0438. \u041e\u0442\u043c\u0435\u0447\u0443, \u0447\u0442\u043e async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u00ab\u043f\u043e\u0432\u0435\u0440\u0445\u00bb \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432. \u042d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u0440\u0443\u0433\u0438\u0435 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438. Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u043a\u0430\u043a \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0430 \u043a\u043e\u0434\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0440\u043e\u043c\u0438\u0441\u044b.<\/p>\n<p>  Async\/Await \u0434\u0430\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435.<\/p>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0437\u043d\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432 \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u043e\u0432 Async\/Await.<\/p>\n<p>  <b>\u0421\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441<\/b><\/p>\n<p>  \u0412 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043e\u043d \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u043b\u043e\u0432: async \u0438 await. \u041f\u0435\u0440\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 await. \u0412 \u043b\u044e\u0431\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<pre><code class=\"javascript\">\/\/ With function declaration   async function myFn() {   \/\/ await ... }   \/\/ With arrow function   const myFn = async () =&gt; {   \/\/ await ... }   function myFn() {   \/\/ await fn(); (Syntax Error since no async) }  <\/code><\/pre>\n<p>  Async \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u0430\u043c\u043e\u043c \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u0435\u043b\u043e\u0447\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u2014 \u043c\u0435\u0436\u0434\u0443 \u0437\u043d\u0430\u043a\u043e\u043c \u00ab=\u00bb \u0438 \u0441\u043a\u043e\u0431\u043a\u0430\u043c\u0438.<\/p>\n<p>  \u042d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043b\u0438\u0431\u043e \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<pre><code class=\"javascript\">\/\/ As an object's method   const obj = {   async getName() {     return fetch('https:\/\/www.example.com');   } }   \/\/ In a class   class Obj {   async getResource() {     return fetch('https:\/\/www.example.com');   } }<\/code><\/pre>\n<p>  NB! \u0421\u0442\u043e\u0438\u0442 \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0433\u0435\u0442\u0442\u0435\u0440\u044b\/\u0441\u0435\u0442\u0442\u0435\u0440\u044b \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438.<\/p>\n<p>  <b>\u0421\u0435\u043c\u0430\u043d\u0442\u0438\u043a\u0430 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/b><\/p>\n<p>  Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 JS-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u043e \u0435\u0441\u0442\u044c \u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0422\u0430\u043a, async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u043f\u0440\u043e\u043c\u0438\u0441\u044b:<\/p>\n<pre><code class=\"javascript\">async function fn() {   return 'hello'; } fn().then(console.log) \/\/ hello<\/code><\/pre>\n<p>  \u0412 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, fn \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 hello. \u041d\u0443 \u0430 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0431\u0435\u0440\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u043c\u0438\u0441 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430.<\/p>\n<p>  \u0412\u043e\u0442 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0431\u0435\u0437 Async:<\/p>\n<pre><code class=\"javascript\">function fn() {   return Promise.resolve('hello'); }   fn().then(console.log); \/\/ hello<\/code><\/pre>\n<p>  \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u043c\u0438\u0441\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u00ab\u0432\u0440\u0443\u0447\u043d\u0443\u044e\u00bb. \u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0431\u0435\u0440\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u043c\u0438\u0441.<\/p>\n<p>  \u0412 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u2014 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432, async-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043e\u0431\u0435\u0440\u0442\u044b\u0432\u0430\u044f \u0435\u0433\u043e \u0432 \u043f\u0440\u043e\u043c\u0438\u0441. \u0412 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0440\u043e\u043c\u0438\u0441\u0430, \u0435\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0432\u043e\u043c \u043f\u0440\u043e\u043c\u0438\u0441\u0435.<\/p>\n<pre><code class=\"javascript\">const p = Promise.resolve('hello') p instanceof Promise; \/\/ true   Promise.resolve(p) === p; \/\/ true  <\/code><\/pre>\n<p>  \u041d\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u043d\u0443\u0442\u0440\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430?<\/p>\n<pre><code class=\"javascript\">async function foo() {   throw Error('bar'); }   foo().catch(console.log);<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438 \u043e\u043d\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u0430, foo() \u0432\u0435\u0440\u043d\u0435\u0442 \u043f\u0440\u043e\u043c\u0438\u0441 \u0441 \u0440\u0435\u0434\u0436\u0435\u043a\u0442\u043e\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u043e Promise.resolve \u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f Promise.reject, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043e\u0448\u0438\u0431\u043a\u0443.<\/p>\n<p>  Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0430\u044e\u0442 \u043f\u0440\u043e\u043c\u0438\u0441, \u0432\u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  \u0410\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c await .<\/p>\n<p>  Await \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u0422\u0430\u043a, \u0435\u0441\u043b\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u043c, async-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u043c\u0438\u0441\u0430. \u0412 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u043c, \u043e\u043d\u043e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u043c\u0438\u0441 \u0447\u0435\u0440\u0435\u0437 Promise.resolve \u0438 \u043f\u043e\u0442\u043e\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"javascript\">\/\/ utility function to cause delay \/\/ and get random value   const delayAndGetRandom = (ms) =&gt; {   return new Promise(resolve =&gt; setTimeout(     () =&gt; {       const val = Math.trunc(Math.random() * 100);       resolve(val);     }, ms   )); };   async function fn() {   const a = await 9;   const b = await delayAndGetRandom(1000);   const c = await 5;   await delayAndGetRandom(1000);     return a + b * c; }   \/\/ Execute fn fn().then(console.log);<\/code><\/pre>\n<p>  \u0410 \u0432\u043e\u0442 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 fn-\u0444\u0443\u043d\u043a\u0446\u0438\u044f.<\/p>\n<ul>\n<li>\u041f\u043e\u0441\u043b\u0435 \u0435\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438\u0437 const a = await 9; \u0432 const a = await Promise.resolve(9);.<\/li>\n<li>\u041f\u043e\u0441\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Await \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u043a\u0430 \u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (\u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u044d\u0442\u043e 9).<\/li>\n<li>delayAndGetRandom(1000) \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 fn-\u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0441\u0430\u043c\u0430 (\u043f\u043e\u0441\u043b\u0435 1 \u0441\u0435\u043a\u0443\u043d\u0434\u044b). \u042d\u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 fn-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 1 \u0441\u0435\u043a\u0443\u043d\u0434\u0443.<\/li>\n<li>delayAndGetRandom(1000) \u0447\u0435\u0440\u0435\u0437 resolve \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 b.<\/li>\n<li>\u041d\u0443 \u0430 \u0441\u043b\u0443\u0447\u0430\u0439 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u0441\u043b\u0443\u0447\u0430\u044e \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u043d\u043e \u0442\u0435\u043f\u0435\u0440\u044c delayAndGetRandom(1000) \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/li>\n<li>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435 a + b * c. \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0436\u0435 \u043e\u0431\u0435\u0440\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u043e\u043c\u0438\u0441 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Promise.resolve \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439.<\/li>\n<\/ul>\n<p>  \u042d\u0442\u0438 \u043f\u0430\u0443\u0437\u044b \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432 ES6, \u043d\u043e \u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u0442\u044c <a href=\"https:\/\/codeburst.io\/understanding-generators-in-es6-javascript-with-examples-6728834016d5\">\u0441\u0432\u043e\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u044b<\/a>.<\/p>\n<h3>\u0420\u0435\u0448\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443<\/h3>\n<p>  \u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b\u043b\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u0430 \u0432\u044b\u0448\u0435.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/40b\/ec5\/c45\/40bec5c4564dfabbe1810db3d4a60c2d.png\"><\/p>\n<p>  \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 finishMyTask \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Await \u0434\u043b\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0442\u0430\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u0430\u043a queryDatabase, sendEmail, logTaskInFile \u0438 \u0434\u0440\u0443\u0433\u0438\u0445. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0442\u0435\u043c, \u0433\u0434\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c \u043f\u0440\u043e\u043c\u0438\u0441\u044b, \u0441\u0442\u0430\u043d\u0435\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u043e. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432\u0435\u0440\u0441\u0438\u044f \u0441 Async\/Await \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432 \u0438 \u0446\u0435\u043f\u043e\u0447\u0435\u043a \u0432\u0440\u043e\u0434\u0435 .then\/.catch.<\/p>\n<p>  \u0412\u043e\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0432\u044b\u0432\u043e\u0434\u043e\u043c \u0447\u0438\u0441\u0435\u043b, \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430.<\/p>\n<pre><code class=\"javascript\">const wait = (i, ms) =&gt; new Promise(resolve =&gt; setTimeout(() =&gt; resolve(i), ms));   \/\/ Implementation One (Using for-loop) const printNumbers = () =&gt; new Promise((resolve) =&gt; {   let pr = Promise.resolve(0);   for (let i = 1; i &lt;= 10; i += 1) {     pr = pr.then((val) =&gt; {       console.log(val);       return wait(i, Math.random() * 1000);     });   }   resolve(pr); });   \/\/ Implementation Two (Using Recursion)   const printNumbersRecursive = () =&gt; {   return Promise.resolve(0).then(function processNextPromise(i) {       if (i === 10) {       return undefined;     }       return wait(i, Math.random() * 1000).then((val) =&gt; {       console.log(val);       return processNextPromise(i + 1);     });   }); };<\/code><\/pre>\n<p>  \u0410 \u0432\u043e\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c async-\u0444\u0443\u043d\u043a\u0446\u0438\u0439.<\/p>\n<pre><code class=\"javascript\">async function printNumbersUsingAsync() {   for (let i = 0; i &lt; 10; i++) {     await wait(i, Math.random() * 1000);     console.log(i);   } }<\/code><\/pre>\n<p>  <b>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/b><\/p>\n<p>  \u041d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u043e\u0431\u0435\u0440\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 rejected \u043f\u0440\u043e\u043c\u0438\u0441. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u0432 async-\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e try\/catch \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0448\u0438\u0431\u043e\u043a.<\/p>\n<pre><code class=\"javascript\">async function canRejectOrReturn() {   \/\/ wait one second   await new Promise(res =&gt; setTimeout(res, 1000));   \/\/ Reject with ~50% probability   if (Math.random() &gt; 0.5) {     throw new Error('Sorry, number too big.')   }   return 'perfect number'; }<\/code><\/pre>\n<p>  canRejectOrReturn() \u2014 \u044d\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043b\u0438\u0431\u043e \u0443\u0434\u0430\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (\u201cperfect number\u201d), \u043b\u0438\u0431\u043e \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \u043e\u0448\u0438\u0431\u043a\u043e\u0439 (\u201cSorry, number too big\u201d).<\/p>\n<pre><code class=\"javascript\">async function foo() {   try {     await canRejectOrReturn();   } catch (e) {     return 'error caught';   } }<\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 canRejectOrReturn, \u0442\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0432\u043b\u0435\u0447\u0435\u0442 \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 catch. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f foo \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043b\u0438\u0431\u043e \u0441 undefined (\u043a\u043e\u0433\u0434\u0430 \u0432 \u0431\u043b\u043e\u043a\u0435 try \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f), \u043b\u0438\u0431\u043e \u0441 error caught. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 try\/catch \u0437\u0430\u0439\u043c\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u043e\u0439 \u0441\u0430\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 foo.<\/p>\n<p>  \u0412\u043e\u0442 \u0435\u0449\u0435 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"javascript\">async function foo() {   try {     return canRejectOrReturn();   } catch (e) {     return 'error caught';   } }<\/code><\/pre>\n<p>  \u0421\u0442\u043e\u0438\u0442 \u0443\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0437 foo \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f canRejectOrReturn. Foo \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0431\u043e perfect number, \u043b\u0438\u0431\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430 Error (\u201cSorry, number too big\u201d). \u0411\u043b\u043e\u043a catch \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f.<\/p>\n<p>  \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e foo \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u043e\u043c\u0438\u0441, \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u0442 canRejectOrReturn. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 foo \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u043b\u044f canRejectOrReturn. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u0442\u044c \u0432\u0441\u0435\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0441\u0442\u0440\u043e\u043a:<\/p>\n<pre><code class=\"javascript\">try {     const promise = canRejectOrReturn();     return promise; }<\/code><\/pre>\n<p>  \u0410 \u0432\u043e\u0442 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0435 await \u0438 return:<\/p>\n<pre><code class=\"javascript\">async function foo() {   try {     return await canRejectOrReturn();   } catch (e) {     return 'error caught';   } }<\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0434\u0435 \u0432\u044b\u0448\u0435 foo \u0443\u0434\u0430\u0447\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043a\u0430\u043a \u0441 perfect number, \u0442\u0430\u043a \u0438 \u0441 error caught. \u0417\u0434\u0435\u0441\u044c \u043e\u0442\u043a\u0430\u0437\u043e\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u041d\u043e foo \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0441 canRejectOrReturn, \u0430 \u043d\u0435 \u0441 undefined. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0443\u0431\u0435\u0434\u0438\u043c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c, \u0443\u0431\u0440\u0430\u0432 \u0441\u0442\u0440\u043e\u043a\u0443 return await canRejectOrReturn():<\/p>\n<pre><code class=\"javascript\">try {     const value = await canRejectOrReturn();     return value; } \/\/ \u2026<\/code><\/pre>\n<p>  <\/p>\n<h3>\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438<\/h3>\n<p>  \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Async\/Await \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043e\u0448\u0438\u0431\u043a\u0430\u043c.<\/p>\n<p>  <b>\u0417\u0430\u0431\u044b\u0442\u044b\u0439 await<\/b><\/p>\n<p>  \u0422\u0430\u043a\u043e\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u2014 \u043f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u043c \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e await:<\/p>\n<pre><code class=\"javascript\">async function foo() {   try {     canRejectOrReturn();   } catch (e) {     return 'caught';   } }<\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0434\u0435, \u043a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0435\u0442 \u043d\u0438 await, \u043d\u0438 return. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 foo \u0432\u0441\u0435\u0433\u0434\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0441 undefined \u0431\u0435\u0437 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438 \u0432 1 \u0441\u0435\u043a\u0443\u043d\u0434\u0443. \u041d\u043e \u043f\u0440\u043e\u043c\u0438\u0441 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f. \u0415\u0441\u043b\u0438 \u0436\u0435 \u043e\u043d \u0432\u044b\u0434\u0430\u0435\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u0438\u043b\u0438 \u0440\u0435\u0434\u0436\u0435\u043a\u0442, \u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f UnhandledPromiseRejectionWarning.<\/p>\n<p>  <b>Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445<\/b><\/p>\n<p>  Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 .map \u0438\u043b\u0438 .filter \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e fetchPublicReposCount(username), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u043d\u0430 GitHub \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u044c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u044b. \u0412\u043e\u0442 \u043a\u043e\u0434 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<pre><code class=\"javascript\">const url = 'https:\/\/api.github.com\/users';   \/\/ Utility fn to fetch repo counts const fetchPublicReposCount = async (username) =&gt; {   const response = await fetch(`${url}\/${username}`);   const json = await response.json();   return json['public_repos']; }<\/code><\/pre>\n<p>  \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u044b \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u044b ArfatSalman, octocat, norvig. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c:<\/p>\n<pre><code class=\"javascript\">const users = [   'ArfatSalman',   'octocat',   'norvig' ];   const counts = users.map(async username =&gt; {   const count = await fetchPublicReposCount(username);   return count; });<\/code><\/pre>\n<p>  \u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 Await \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 .map. \u0417\u0434\u0435\u0441\u044c counts \u2014 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432, \u043d\u0443 \u0430 .map \u2014 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u044b\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<p>  <b>\u0427\u0440\u0435\u0437\u043c\u0435\u0440\u043d\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 await<\/b><\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">async function fetchAllCounts(users) {   const counts = [];   for (let i = 0; i &lt; users.length; i++) {     const username = users[i];     const count = await fetchPublicReposCount(username);     counts.push(count);   }   return counts; }<\/code><\/pre>\n<p>  \u0417\u0434\u0435\u0441\u044c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e count \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u043f\u043e, \u0437\u0430\u0442\u0435\u043c \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 counts. \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043a\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u043a\u0430 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0431\u0443\u0434\u0443\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c.<\/p>\n<p>  \u0415\u0441\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0445\u043e\u0434\u0438\u0442 \u043e\u043a\u043e\u043b\u043e 300 \u043c\u0441, \u0442\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0435\u043a\u0443\u043d\u0434\u0430, \u0437\u0430\u0442\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0447\u0438\u0441\u043b\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439. \u041d\u043e \u0440\u0430\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0440\u0435\u043f\u043e \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430, \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0442\u044c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 .map \u0438 Promise.all:<\/p>\n<pre><code class=\"javascript\">async function fetchAllCounts(users) {   const promises = users.map(async username =&gt; {     const count = await fetchPublicReposCount(username);     return count;   });   return Promise.all(promises); }<\/code><\/pre>\n<p>  Promise.all \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u043c\u0438\u0441\u0430. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438\u043b\u0438 \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u0434\u0436\u0435\u043a\u0442\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f. \u041c\u043e\u0436\u0435\u0442 \u0441\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0442\u0430\u043a, \u0447\u0442\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u043d\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u2014 \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c p-map.<\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>  Async-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438. \u041d\u0443 \u0430 \u0434\u043b\u044f \u0430\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f async-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441\u0442\u043e\u0438\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <a href=\"https:\/\/github.com\/tc39\/proposal-async-iteration\" rel=\"nofollow noopener noreferrer\">Async Iterators<\/a>. JavaScript-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043e\u043b\u0436\u0435\u043d \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u044d\u0442\u043e\u043c.<\/p>\n<blockquote><p><b>Skillbox \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442:<\/b><\/p>\n<ul>\n<li>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u0443\u0440\u0441 <a href=\"https:\/\/skillbox.ru\/agima\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=AGIMA&amp;utm_content=articles&amp;utm_term=async\">\u00ab\u041c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a PRO\u00bb<\/a>.<\/li>\n<li>\u041f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u043e\u0439 \u043e\u043d\u043b\u0430\u0439\u043d-\u043a\u0443\u0440\u0441 <a href=\"https:\/\/skillbox.ru\/python-data\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=PTNANA&amp;utm_content=articles&amp;utm_term=async\">\u00ab\u0410\u043d\u0430\u043b\u0438\u0442\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 Python\u00bb<\/a>.<\/li>\n<li>\u0414\u0432\u0443\u0445\u043b\u0435\u0442\u043d\u0438\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u0443\u0440\u0441 <a href=\"https:\/\/iamwebdev.skillbox.ru\/?utm_source=skillbox.media&amp;utm_medium=habr.com&amp;utm_campaign=WEBDEVPRO&amp;utm_content=articles&amp;utm_term=async\">\u00ab\u042f \u2014 \u0412\u0435\u0431-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a PRO\u00bb<\/a>.<\/li>\n<\/ul>\n<\/blockquote>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/skillbox\/blog\/458950\/\"> https:\/\/habr.com\/ru\/company\/skillbox\/blog\/458950\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/2c8\/f4a\/252\/2c8f4a252d33ee663665fd7df02b45ed.jpg\"> <\/p>\n<p>  \u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445 Async\/Await \u0432 JavaScript. \u0412 \u0446\u0435\u043b\u043e\u043c, Async\/Await \u2014 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0414\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0438\u0441\u0430\u043b\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u043e\u0432 \u0438 \u043f\u0440\u043e\u043c\u0438\u0441\u043e\u0432. \u0410\u0432\u0442\u043e\u0440 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 Async\/Await, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-291715","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291715"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291715\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}