{"id":338012,"date":"2022-09-06T15:01:28","date_gmt":"2022-09-06T15:01:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=338012"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=338012","title":{"rendered":"<span>10 \u0437\u0430\u0434\u0430\u0447 \u0441 JavaScript Promise \u0434\u043b\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u043a \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a4a\/846\/18c\/a4a84618cb865ebd64a9dcf6956d3814.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a4a\/846\/18c\/a4a84618cb865ebd64a9dcf6956d3814.png\"\/><figcaption><\/figcaption><\/figure>\n<p>Promise \u2014 \u044d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c JavaScript \u043a\u0430\u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0432\u0430\u043c \u044d\u0442\u043e \u0438\u043b\u0438 \u043d\u0435\u0442, \u043f\u043e\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u0438\u0432\u043e\u0436\u0443 10 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430 \u0441 Promise, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u043c. \u0413\u043e\u0442\u043e\u0432\u044b? \u041d\u0430\u0447\u043d\u0435\u043c!<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21161: \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0421 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043b\u0435\u0433\u043a\u043e.<\/p>\n<p>\u0427\u0442\u043e \u043c\u044b \u0437\u043d\u0430\u0435\u043c:<\/p>\n<ul>\n<li>\n<p>\u0411\u043b\u043e\u043a\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>new Promise(callback)<\/code>, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435.<\/p>\n<\/li>\n<\/ul>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0418\u0442\u0430\u043a, \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c <code>start<\/code><em>, <\/em><code>1<\/code><em>, <\/em><code>end<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21162: .then()<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2) })  promise1.then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 <code>.then()<\/code>.<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 JavaScript \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439.<\/p>\n<p>\u041f\u0440\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u0441 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/17d\/a9e\/93f\/17da9e93f5a20798ba7a86cccf42e180.png\" width=\"1400\" height=\"1244\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/17d\/a9e\/93f\/17da9e93f5a20798ba7a86cccf42e180.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2) })  promise1.then(res => {   console.log(res) })  console.log('end');;<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0432\u044b\u0432\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0435\u0442<strong> <\/strong><code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> <\/em>\u0438<em> <\/em><code>2<\/code><em> .<\/em><\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21163: resolve()<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2)   console.log(3) })  promise1.then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439; \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 <code>resolve(2)<\/code> \u0435\u0441\u0442\u044c <code>console.log(3)<\/code>.<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 <code>resolve<\/code> \u043d\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041a\u043e\u0434, \u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0437\u0430 \u043d\u0438\u043c, \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f.<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>3<\/code><em> , <\/em><code>end<\/code><em> <\/em>\u0438<em> <\/em><code>2<\/code><em> .<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2)   console.log(3) })  promise1.then(res => {   console.log(res) })  console.log('end');; <\/code><\/pre>\n<p>\u042f \u043d\u0435\u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u043c\u043d\u0435\u043d\u0438\u0435\u043c, \u0431\u0443\u0434\u0442\u043e <code>resolve<\/code> \u043f\u0440\u0435\u0440\u0432\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u044e \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0434\u0435\u0441\u044c.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21164: resolve() \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  promise1.then(res => {   console.log(2) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 resolve \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>promise1<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (pending). \u0422\u0430\u043a \u0447\u0442\u043e <code>promise1.then(\u2026)<\/code> \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0441\u044f.<em> <\/em><code>2<\/code> \u043d\u0435 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438.<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0441\u0442\u0430\u043d\u0435\u0442<strong> <\/strong><code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> .<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  promise1.then(res => {   console.log(2) })  console.log('end');;<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21165: \u041d\u0435\u0447\u0442\u043e, \u0441\u0431\u0438\u0432\u0430\u044e\u0449\u0435\u0435 \u0441 \u0442\u043e\u043b\u043a\u0443<\/h3>\n<pre><code class=\"javascript\">console.log('start')  const fn = () => (new Promise((resolve, reject) => {   console.log(1);   resolve('success') }))  console.log('middle')  fn().then(res => {   console.log(res) })  console.log('end')<\/code><\/pre>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u044b\u0442\u0443\u0435\u043c\u044b\u0445, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441, \u0438 \u044d\u0442\u043e <code>fn<\/code>.<\/p>\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0442\u043e\u0433\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u043e\u0435\u0432 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043d\u0430\u0448\u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/123\/88f\/427\/12388f427cffabb6fc574bbd2597dd1e.png\" width=\"1400\" height=\"1321\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/123\/88f\/427\/12388f427cffabb6fc574bbd2597dd1e.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code><em> , <\/em><code>middle<\/code><em>, <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> <\/em><code>\u0438<\/code><em> <\/em><code>success<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  const fn = () => (new Promise((resolve, reject) => {   console.log(1);   resolve('success') }))  console.log('middle')  fn().then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21166: \u0441 Fulfilling Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start')  Promise.resolve(1).then((res) => {   console.log(res) })  Promise.resolve(2).then((res) => {   console.log(res) })  console.log('end')<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0417\u0434\u0435\u0441\u044c <code>Promise.resolve(1)<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Promise, \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e <code>fulfilled<\/code>, \u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0432\u0435\u043d <code>1<\/code> . \u042d\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/254\/f14\/578\/254f1457848db4ea409658bac00edbb0.png\" width=\"1030\" height=\"356\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/254\/f14\/578\/254f1457848db4ea409658bac00edbb0.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442<em> <\/em><code>start<\/code><em> , <\/em><code>end<\/code><em> , <\/em><code>1<\/code><em> \u0438 <\/em><code>2<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  Promise.resolve(1).then((res) => {   console.log(res) })  Promise.resolve(2).then((res) => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<hr\/>\n<p>\u041d\u0443 \u0447\u0442\u043e, \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u044d\u0442\u043e \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438?<\/p>\n<p>\u042d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0430\u043b\u043e. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c Promise \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441 <code>setTimeout<\/code>. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<p>\u0413\u043e\u0442\u043e\u0432\u044b? \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21167: setTimeout vs Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start')  setTimeout(() => {   console.log('setTimeout') })  Promise.resolve().then(() => {   console.log('resolve') })  console.log('end')<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0430\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 JavaScript \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u043e \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f.<\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u044f \u0434\u0430\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u0430\u0437\u0443.<\/p>\n<p>\u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438, \u0447\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u044b\u0437\u043e\u0432\u0430, \u0442\u0430\u043a \u0432 \u043a\u0430\u043a\u043e\u043c \u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u044d\u0442\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u0438?<\/p>\n<p>\u041a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u043e\u0442, \u043a\u0442\u043e \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442 \u043f\u0435\u0440\u0432\u044b\u043c, \u0431\u0443\u0434\u0435\u0442 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u043f\u0435\u0440\u0432\u044b\u043c. \u0427\u0442\u043e \u0436, \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0434\u0430, \u043d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0434\u0432\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e?<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u0442\u0430\u0439\u043c\u0435\u0440 <code>setTimeout<\/code> \u0440\u0430\u0432\u0435\u043d 0 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u043c, \u0430 <code>Promise.resolve()<\/code> \u0442\u0430\u043a\u0436\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Promise \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041e\u0431\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u044c\u044f \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439?<\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0436\u0443\u043d\u044b \u043c\u043e\u0433\u0443\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e <code>setTimeout<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d <code>setTimeout<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c <code>resolve<\/code>. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u0432\u0435\u0440\u043d\u043e.<\/p>\n<p>\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0432\u0435\u0449\u0438 \u041d\u0415 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u00ab\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b \u2014 \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\u00bb, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0440\u0430\u0444\u0438\u043a.<\/p>\n<h4>\u041f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442<\/h4>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0434\u0435\u043b\u0438\u043c \u0432\u0435\u0441\u044c \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442 \u043d\u0430 \u0434\u0432\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0436\u0430\u0440\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0438 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0435\u0445\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043b\u044e\u0434\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043a\u0438, \u043c\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u043f\u043e\u0436\u0430\u0440\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0438 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438. \u0410\u0432\u0442\u043e\u043c\u043e\u0431\u0438\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442. \u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e: \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/629\/fda\/def\/629fdadef9f9aa1977f84b463f247199.png\" width=\"1400\" height=\"990\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/629\/fda\/def\/629fdadef9f9aa1977f84b463f247199.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0\u0412 JavaScript EventLoop \u0442\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430.<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>Promise<\/code>, <code>ObjectObserver<\/code>, <code>MutationObserver<\/code>, <code>process.nextTick<\/code>, <code>async\/await<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>setTimeout<\/code>, <code>setInterval<\/code> \u0438 <code>XHR<\/code>.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/025\/2f1\/23b\/0252f123b11ef7a3278dc8a60f55d1f9.png\" width=\"1400\" height=\"758\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/025\/2f1\/23b\/0252f123b11ef7a3278dc8a60f55d1f9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0425\u043e\u0442\u044f <code>setTimeout<\/code> \u0438 <code>Promise.resolve()<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0438 \u0434\u0430\u0436\u0435 \u043a\u043e\u0434 <code>setTimeout<\/code> \u0435\u0449\u0435 \u0432\u043f\u0435\u0440\u0435\u0434\u0438, \u043d\u043e \u0438\u0437-\u0437\u0430 \u043d\u0438\u0437\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0430\u044f\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0437\u0436\u0435.<\/p>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b23\/27e\/ad6\/b2327ead6dd93cab59ee381fde497430.png\" width=\"623\" height=\"1600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b23\/27e\/ad6\/b2327ead6dd93cab59ee381fde497430.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code> , <code>end<\/code> , <code>resolve<\/code> \u0438 <code>setTimeout<\/code>.<\/p>\n<pre><code class=\"javascript\">console.log('start')  setTimeout(() => {   console.log('setTimeout') })  Promise.resolve().then(() => {   console.log('resolve') })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21168: \u041c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">const promise = new Promise((resolve, reject) => {   console.log(1);   setTimeout(() => {     console.log(\"timerStart\");     resolve(\"success\");     console.log(\"timerEnd\");   }, 0);   console.log(2); });  promise.then((res) => {   console.log(res); });  console.log(4);<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043a\u043e\u0434.<\/p>\n<p>\u041d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u0438 \u0442\u0440\u0438 \u0448\u0430\u0433\u0430:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u043a\u043e\u0434 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u043a\u043e\u0434 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/655\/988\/9c8\/6559889c8db17c730ccde84efcdc79a3.png\" width=\"1400\" height=\"1241\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/655\/988\/9c8\/6559889c8db17c730ccde84efcdc79a3.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f <code>1<\/code> , <code>2<\/code>\u00a0 \u0438 <code>4<\/code> .<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/020\/2e2\/62c\/0202e262cccda3e560799aeac61c8844.png\" width=\"1400\" height=\"1249\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/020\/2e2\/62c\/0202e262cccda3e560799aeac61c8844.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0\u041d\u043e \u0432\u043e\u0442 \u043b\u043e\u0432\u0443\u0448\u043a\u0430: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 Promise \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (pending), \u043a\u043e\u0434 \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c12\/d19\/a10\/c12d19a1082cea6b8048fdf8ab03613c.png\" width=\"1400\" height=\"1502\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c12\/d19\/a10\/c12d19a1082cea6b8048fdf8ab03613c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <code>promise<\/code>  \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f <code>fulfilled<\/code> .<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Event Loop \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/3ec\/50b\/6e0\/3ec50b6e08ee283ddd9563086b4754fa.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/3ec\/50b\/6e0\/3ec50b6e08ee283ddd9563086b4754fa.png\"\/><figcaption><\/figcaption><\/figure>\n<pre><code class=\"javascript\">const promise = new Promise((resolve, reject) => {   console.log(1);   setTimeout(() => {     console.log(\"timerStart\");     resolve(\"success\");     console.log(\"timerEnd\");   }, 0);   console.log(2); }); promise.then((res) => {   console.log(res); }); console.log(4);;<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21169: \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u043c\u0435\u0436\u0434\u0443 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>\u0427\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">const timer1 = setTimeout(() => {   console.log('timer1');      const promise1 = Promise.resolve().then(() => {     console.log('promise1')   }) }, 0)  const timer2 = setTimeout(() => {   console.log('timer2') }, 0)<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043d\u043e\u0432\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0426\u0438\u043a\u043b \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \/ \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u043e \u044d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u0432\u0435\u0440\u043d\u043e. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 (\u0432\u043d\u043e\u0432\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435) \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0426\u0438\u043a\u043b \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \/ \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1a6\/2c0\/af0\/1a62c0af08cf310ca52b1dec78e79fd1.png\" width=\"320\" height=\"937\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1a6\/2c0\/af0\/1a62c0af08cf310ca52b1dec78e79fd1.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418\u043b\u0438 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8e4\/11d\/877\/8e411d8773107dca77978aebfa467d1f.png\" width=\"1400\" height=\"319\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8e4\/11d\/877\/8e411d8773107dca77978aebfa467d1f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>Promise.then<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0432\u0442\u043e\u0440\u043e\u0433\u043e <code>setTimeout<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430, \u0438 \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0432\u0440\u0435\u0437\u0430\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d09\/50e\/cf5\/d0950ecf5a43fd6c4c3a50f404e6ab06.png\" width=\"704\" height=\"1600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d09\/50e\/cf5\/d0950ecf5a43fd6c4c3a50f404e6ab06.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fd0\/61e\/986\/fd061e986d287745dc08010356ec92c8.png\" width=\"1400\" height=\"749\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fd0\/61e\/986\/fd061e986d287745dc08010356ec92c8.png\"\/><figcaption><\/figcaption><\/figure>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u211610: \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u0441 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<p>\u0427\u0442\u043e \u0436, \u044d\u0442\u043e \u043d\u0430\u0448\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0442\u043e \u0432\u0430\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 Promise \u0443\u0436\u0435 \u043d\u0430 \u0432\u044b\u0441\u043e\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435. \u0418 \u043e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u043e\u0447\u043d\u043e \u043d\u0435 \u0441\u0442\u0430\u043d\u0443\u0442 \u0434\u043b\u044f \u0432\u0430\u0441 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u0427\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = Promise.resolve().then(() => {   console.log('promise1');   const timer2 = setTimeout(() => {     console.log('timer2')   }, 0) });  const timer1 = setTimeout(() => {   console.log('timer1')   const promise2 = Promise.resolve().then(() => {     console.log('promise2')   }) }, 0)  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u0443\u0440\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c.<\/p>\n<p>\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043c\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435:<\/p>\n<ol>\n<li>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438<\/p>\n<\/li>\n<li>\n<p>\u041f\u0435\u0440\u0432\u0430\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430<\/p>\n<\/li>\n<li>\n<p>\u0412\u0441\u0435 \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438<\/p>\n<\/li>\n<li>\n<p>\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u00a0<\/p>\n<\/li>\n<\/ol>\n<p>\u2026<\/p>\n<p>\u0418\u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432\u0435\u0441\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/28a\/75f\/07f\/28a75f07f8359cf7344711ac3fc1b586.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/28a\/75f\/07f\/28a75f07f8359cf7344711ac3fc1b586.png\"\/><figcaption><\/figcaption><\/figure>\n<ol start=\"2\">\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a7d\/a2d\/06a\/a7da2d06a8f0cc1eb4d77cceda00162a.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a7d\/a2d\/06a\/a7da2d06a8f0cc1eb4d77cceda00162a.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<ol start=\"3\">\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<\/li>\n<\/ol>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/142\/786\/8a2\/1427868a217968f561a4c431f26d62b0.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/142\/786\/8a2\/1427868a217968f561a4c431f26d62b0.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435. \u041d\u0430 \u044d\u0442\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447 \u043d\u043e\u0432\u0443\u044e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443.<\/p>\n<p>4. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0432\u0441\u0435 \u0432\u043d\u043e\u0432\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/8d7\/33e\/d82\/8d733ed82f0946319a9b0470c986a6f9.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8d7\/33e\/d82\/8d733ed82f0946319a9b0470c986a6f9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>5. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/18a\/fad\/1a9\/18afad1a9058f249af3994a592ef345f.png\" width=\"1400\" height=\"1598\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18a\/fad\/1a9\/18afad1a9058f249af3994a592ef345f.png\"\/><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0432\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0438\u043c.<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = Promise.resolve().then(() => {   console.log('promise1');   const timer2 = setTimeout(() => {     console.log('timer2')   }, 0) });  const timer1 = setTimeout(() => {   console.log('timer1')   const promise2 = Promise.resolve().then(() => {     console.log('promise2')   }) }, 0)  console.log('end');;<\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u0442\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u0430:<\/p>\n<ol>\n<li>\n<p>\u0418\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 JavaScript \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439.<\/p>\n<\/li>\n<li>\n<p>\u041c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u043d\u0430\u0434 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438.<\/p>\n<\/li>\n<\/ol>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7eb\/612\/1dd\/7eb6121dd459340ddfbabb08f588eca7.png\" width=\"1400\" height=\"758\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7eb\/612\/1dd\/7eb6121dd459340ddfbabb08f588eca7.png\"\/><figcaption><\/figcaption><\/figure>\n<p>3. \u041c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u0432\u0440\u0435\u0437\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432 Event Loop.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a0a\/c8f\/f8b\/a0ac8ff8b23c7422cb2042d8d41c2b1c.png\" width=\"490\" height=\"1600\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a0a\/c8f\/f8b\/a0ac8ff8b23c7422cb2042d8d41c2b1c.png\"\/><figcaption><\/figcaption><\/figure>\n<hr\/>\n<blockquote>\n<p>\u0417\u0430\u0432\u0442\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a \u00ab<strong>\u0424\u043b\u0435\u043a\u0441\u044b \u0438 \u0433\u0440\u0438\u0434\u044b \u2014 \u0432 \u0447\u0435\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435?<\/strong>\u00bb. \u041d\u0430 \u0432\u0435\u0431\u0438\u043d\u0430\u0440\u0435 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0434\u0432\u0430 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u043a\u0435\u0442\u0430. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0438\u0445 \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b, \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f. \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f <a href=\"https:\/\/otus.pw\/sdqj\/\"><strong>\u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435.<\/strong><\/a><\/p>\n<\/blockquote>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><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\/otus\/blog\/686670\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/686670\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>Promise \u2014 \u044d\u0442\u043e \u043e\u0442\u043b\u0438\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c JavaScript \u043a\u0430\u043a \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0432\u0430\u043c \u044d\u0442\u043e \u0438\u043b\u0438 \u043d\u0435\u0442, \u043f\u043e\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u0440\u0438\u0432\u043e\u0436\u0443 10 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430 \u0441 Promise, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u043c. \u0413\u043e\u0442\u043e\u0432\u044b? \u041d\u0430\u0447\u043d\u0435\u043c!<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21161: \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0421 \u043f\u0435\u0440\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043b\u0435\u0433\u043a\u043e.<\/p>\n<p>\u0427\u0442\u043e \u043c\u044b \u0437\u043d\u0430\u0435\u043c:<\/p>\n<ul>\n<li>\n<p>\u0411\u043b\u043e\u043a\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437.<\/p>\n<\/li>\n<li>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>new Promise(callback)<\/code>, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435.<\/p>\n<\/li>\n<\/ul>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0418\u0442\u0430\u043a, \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c <code>start<\/code><em>, <\/em><code>1<\/code><em>, <\/em><code>end<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21162: .then()<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2) })  promise1.then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 <code>.then()<\/code>.<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435 \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 JavaScript \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439.<\/p>\n<p>\u041f\u0440\u0438 \u0441\u0442\u043e\u043b\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u0441 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2) })  promise1.then(res => {   console.log(res) })  console.log('end');;<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0432\u044b\u0432\u043e\u0434\u043e\u043c \u0431\u0443\u0434\u0435\u0442<strong> <\/strong><code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> <\/em>\u0438<em> <\/em><code>2<\/code><em> .<\/em><\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21163: resolve()<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2)   console.log(3) })  promise1.then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439; \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 <code>resolve(2)<\/code> \u0435\u0441\u0442\u044c <code>console.log(3)<\/code>.<\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 <code>resolve<\/code> \u043d\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041a\u043e\u0434, \u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0437\u0430 \u043d\u0438\u043c, \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f.<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>3<\/code><em> , <\/em><code>end<\/code><em> <\/em>\u0438<em> <\/em><code>2<\/code><em> .<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1)   resolve(2)   console.log(3) })  promise1.then(res => {   console.log(res) })  console.log('end');; <\/code><\/pre>\n<p>\u042f \u043d\u0435\u043e\u0434\u043d\u043e\u043a\u0440\u0430\u0442\u043d\u043e \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0441 \u043c\u043d\u0435\u043d\u0438\u0435\u043c, \u0431\u0443\u0434\u0442\u043e <code>resolve<\/code> \u043f\u0440\u0435\u0440\u0432\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u044e \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0434\u0435\u0441\u044c.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21164: resolve() \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  promise1.then(res => {   console.log(2) })  console.log('end');<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 resolve \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>promise1<\/code> \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (pending). \u0422\u0430\u043a \u0447\u0442\u043e <code>promise1.then(\u2026)<\/code> \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0441\u044f.<em> <\/em><code>2<\/code> \u043d\u0435 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438.<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0441\u0442\u0430\u043d\u0435\u0442<strong> <\/strong><code>start<\/code><em> , <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> .<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = new Promise((resolve, reject) => {   console.log(1) })  promise1.then(res => {   console.log(2) })  console.log('end');;<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21165: \u041d\u0435\u0447\u0442\u043e, \u0441\u0431\u0438\u0432\u0430\u044e\u0449\u0435\u0435 \u0441 \u0442\u043e\u043b\u043a\u0443<\/h3>\n<pre><code class=\"javascript\">console.log('start')  const fn = () => (new Promise((resolve, reject) => {   console.log(1);   resolve('success') }))  console.log('middle')  fn().then(res => {   console.log(res) })  console.log('end')<\/code><\/pre>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u0440\u0435\u0434\u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0443\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u044b\u0442\u0443\u0435\u043c\u044b\u0445, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0441, \u0438 \u044d\u0442\u043e <code>fn<\/code>.<\/p>\n<p>\u041f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0442\u043e\u0433\u043e, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u043e\u0435\u0432 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043d\u0430\u0448\u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u044b\u043c\u0438:<\/p>\n<ul>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0430 \u0437\u0430\u0442\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439.<\/p>\n<\/li>\n<li>\n<p>\u0421\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d \u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code><em> , <\/em><code>middle<\/code><em>, <\/em><code>1<\/code><em> , <\/em><code>end<\/code><em> <\/em><code>\u0438<\/code><em> <\/em><code>success<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  const fn = () => (new Promise((resolve, reject) => {   console.log(1);   resolve('success') }))  console.log('middle')  fn().then(res => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21166: \u0441 Fulfilling Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start')  Promise.resolve(1).then((res) => {   console.log(res) })  Promise.resolve(2).then((res) => {   console.log(res) })  console.log('end')<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u0417\u0434\u0435\u0441\u044c <code>Promise.resolve(1)<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 Promise, \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e <code>fulfilled<\/code>, \u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0432\u0435\u043d <code>1<\/code> . \u042d\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442<em> <\/em><code>start<\/code><em> , <\/em><code>end<\/code><em> , <\/em><code>1<\/code><em> \u0438 <\/em><code>2<\/code><em>.<\/em><\/p>\n<pre><code class=\"javascript\">console.log('start')  Promise.resolve(1).then((res) => {   console.log(res) })  Promise.resolve(2).then((res) => {   console.log(res) })  console.log('end');<\/code><\/pre>\n<hr\/>\n<p>\u041d\u0443 \u0447\u0442\u043e, \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u044d\u0442\u043e \u043d\u0435\u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438?<\/p>\n<p>\u042d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0430\u043b\u043e. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c Promise \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441 <code>setTimeout<\/code>. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<p>\u0413\u043e\u0442\u043e\u0432\u044b? \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u043c.<\/p>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21167: setTimeout vs Promise<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start')  setTimeout(() => {   console.log('setTimeout') })  Promise.resolve().then(() => {   console.log('resolve') })  console.log('end')<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441. \u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u043d\u0435\u0433\u043e \u0438 \u043e\u0431\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u0443, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0430\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 JavaScript \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u043e \u0441\u0440\u0435\u0434\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f.<\/p>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u044f \u0434\u0430\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u0430\u0437\u0443.<\/p>\n<p>\u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438, \u0447\u0442\u043e \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u044b\u0437\u043e\u0432\u0430, \u0442\u0430\u043a \u0432 \u043a\u0430\u043a\u043e\u043c \u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u044d\u0442\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u0438?<\/p>\n<p>\u041a\u0442\u043e-\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u043e\u0442, \u043a\u0442\u043e \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442 \u043f\u0435\u0440\u0432\u044b\u043c, \u0431\u0443\u0434\u0435\u0442 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d \u043f\u0435\u0440\u0432\u044b\u043c. \u0427\u0442\u043e \u0436, \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0434\u0430, \u043d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0434\u0432\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e?<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u0442\u0430\u0439\u043c\u0435\u0440 <code>setTimeout<\/code> \u0440\u0430\u0432\u0435\u043d 0 \u0441\u0435\u043a\u0443\u043d\u0434\u0430\u043c, \u0430 <code>Promise.resolve()<\/code> \u0442\u0430\u043a\u0436\u0435 \u0432\u0435\u0440\u043d\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 Promise \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>\u041e\u0431\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u044c\u044f \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439?<\/p>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0436\u0443\u043d\u044b \u043c\u043e\u0433\u0443\u0442 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e <code>setTimeout<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d <code>setTimeout<\/code>, \u0430 \u0437\u0430\u0442\u0435\u043c <code>resolve<\/code>. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u044d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u0432\u0435\u0440\u043d\u043e.<\/p>\n<p>\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0432\u0435\u0449\u0438 \u041d\u0415 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043f\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 \u00ab\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b \u2014 \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\u00bb, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0440\u0430\u0444\u0438\u043a.<\/p>\n<h4>\u041f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442<\/h4>\n<p>\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0434\u0435\u043b\u0438\u043c \u0432\u0435\u0441\u044c \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442 \u043d\u0430 \u0434\u0432\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438:<\/p>\n<ul>\n<li>\n<p>\u041e\u0431\u0449\u0438\u0435 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0422\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442\u043d\u044b\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0436\u0430\u0440\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0438 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438.<\/p>\n<\/li>\n<\/ul>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0435\u0445\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043b\u044e\u0434\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043a\u0438, \u043c\u044b \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \u043f\u043e\u0436\u0430\u0440\u043d\u044b\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0438 \u043c\u0430\u0448\u0438\u043d\u044b \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438. \u0410\u0432\u0442\u043e\u043c\u043e\u0431\u0438\u043b\u0438 \u0441\u043a\u043e\u0440\u043e\u0439 \u043f\u043e\u043c\u043e\u0449\u0438 \u0438\u043c\u0435\u044e\u0442 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442. \u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e: \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u044b.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0\u0412 JavaScript EventLoop \u0442\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430.<\/p>\n<ul>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>Promise<\/code>, <code>ObjectObserver<\/code>, <code>MutationObserver<\/code>, <code>process.nextTick<\/code>, <code>async\/await<\/code>.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0434\u0430\u0447\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440: <code>setTimeout<\/code>, <code>setInterval<\/code> \u0438 <code>XHR<\/code>.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0425\u043e\u0442\u044f <code>setTimeout<\/code> \u0438 <code>Promise.resolve()<\/code> \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u0438 \u0434\u0430\u0436\u0435 \u043a\u043e\u0434 <code>setTimeout<\/code> \u0435\u0449\u0435 \u0432\u043f\u0435\u0440\u0435\u0434\u0438, \u043d\u043e \u0438\u0437-\u0437\u0430 \u043d\u0438\u0437\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0430\u044f\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0437\u0436\u0435.<\/p>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u0412\u044b\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <code>start<\/code> , <code>end<\/code> , <code>resolve<\/code> \u0438 <code>setTimeout<\/code>.<\/p>\n<pre><code class=\"javascript\">console.log('start')  setTimeout(() => {   console.log('setTimeout') })  Promise.resolve().then(() => {   console.log('resolve') })  console.log('end');<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21168: \u041c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438<\/h3>\n<p>\u041a\u0430\u043a\u0438\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">const promise = new Promise((resolve, reject) => {   console.log(1);   setTimeout(() => {     console.log(\"timerStart\");     resolve(\"success\");     console.log(\"timerEnd\");   }, 0);   console.log(2); });  promise.then((res) => {   console.log(res); });  console.log(4);<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u042d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043a\u043e\u0434.<\/p>\n<p>\u041d\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u0438 \u0442\u0440\u0438 \u0448\u0430\u0433\u0430:<\/p>\n<ol>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u043a\u043e\u0434 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u041d\u0430\u0439\u0442\u0438 \u043a\u043e\u0434 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<\/ol>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u044b\u0432\u0435\u0434\u0435\u0442\u0441\u044f <code>1<\/code> , <code>2<\/code>\u00a0 \u0438 <code>4<\/code> .<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0\u041d\u043e \u0432\u043e\u0442 \u043b\u043e\u0432\u0443\u0448\u043a\u0430: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 Promise \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (pending), \u043a\u043e\u0434 \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 <code>promise<\/code>  \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f <code>fulfilled<\/code> .<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Event Loop \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0443:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"javascript\">const promise = new Promise((resolve, reject) => {   console.log(1);   setTimeout(() => {     console.log(\"timerStart\");     resolve(\"success\");     console.log(\"timerEnd\");   }, 0);   console.log(2); }); promise.then((res) => {   console.log(res); }); console.log(4);;<\/code><\/pre>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u21169: \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438<\/h3>\n<p>\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u043c\u0435\u0436\u0434\u0443 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438, \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>\u0427\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">const timer1 = setTimeout(() => {   console.log('timer1');      const promise1 = Promise.resolve().then(() => {     console.log('promise1')   }) }, 0)  const timer2 = setTimeout(() => {   console.log('timer2') }, 0)<\/code><\/pre>\n<h4>\u0410\u043d\u0430\u043b\u0438\u0437<\/h4>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043d\u043e\u0432\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0426\u0438\u043a\u043b \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \/ \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u041d\u043e \u044d\u0442\u043e \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u043d\u0435\u0432\u0435\u0440\u043d\u043e. \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<ol>\n<li>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0430 \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 (\u0432\u043d\u043e\u0432\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435) \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u043c\u0430\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<\/li>\n<li>\n<p>\u0426\u0438\u043a\u043b \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f \/ \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.<\/p>\n<\/li>\n<\/ol>\n<p>\u0422\u0430\u043a:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u043b\u0438 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>Promise.then<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u043a\u043e\u043b\u043b\u0431\u044d\u043a-\u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0432\u0442\u043e\u0440\u043e\u0433\u043e <code>setTimeout<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u043e \u043c\u0438\u043a\u0440\u043e\u0437\u0430\u0434\u0430\u0447\u0430, \u0438 \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0432\u0440\u0435\u0437\u0430\u043d\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u00a0<\/p>\n<h4>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/h4>\n<p>\u00a0<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u211610: \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u0441 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/h3>\n<p>\u0427\u0442\u043e \u0436, \u044d\u0442\u043e \u043d\u0430\u0448\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u044b\u0432\u043e\u0434 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0442\u043e \u0432\u0430\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 Promise \u0443\u0436\u0435 \u043d\u0430 \u0432\u044b\u0441\u043e\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435. \u0418 \u043e\u0434\u043d\u043e\u0442\u0438\u043f\u043d\u044b\u0435 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0438 \u0442\u043e\u0447\u043d\u043e \u043d\u0435 \u0441\u0442\u0430\u043d\u0443\u0442 \u0434\u043b\u044f \u0432\u0430\u0441 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u0427\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u044d\u0442\u043e\u0442 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430?<\/p>\n<pre><code class=\"javascript\">console.log('start');  const promise1 = Promise.resolve().then(() => {   console.log('promise1');   const timer2 = setTimeout(() => {     console.log('timer2')   }, 0) });  const timer1 = setTimeout(() => {   console.log('timer1')   const promise2 = Promise.resolve().then(() => {     console.log('promise2') <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\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-338012","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338012","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=338012"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/338012\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=338012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=338012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=338012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}