<?xml version="1.0" encoding="utf-8" ?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:tt="http://teletype.in/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"><title>Дмитрий Переверза</title><author><name>Дмитрий Переверза</name></author><id>https://teletype.in/atom/devreverza</id><link rel="self" type="application/atom+xml" href="https://teletype.in/atom/devreverza?offset=0"></link><link rel="alternate" type="text/html" href="https://teletype.in/@devreverza?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=devreverza"></link><link rel="next" type="application/rss+xml" href="https://teletype.in/atom/devreverza?offset=10"></link><link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></link><updated>2026-07-26T13:02:16.827Z</updated><entry><id>devreverza:qeLghBRr8dU</id><link rel="alternate" type="text/html" href="https://teletype.in/@devreverza/qeLghBRr8dU?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=devreverza"></link><title>Про MessageChannel</title><published>2022-06-05T09:44:18.365Z</published><updated>2022-06-05T09:44:18.365Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img2.teletype.in/files/90/f0/90f06ba6-5e4e-4c40-856c-37b9eaea2547.png"></media:thumbnail><category term="javascript" label="javascript"></category><summary type="html">На днях у меня возник вопрос, а в какой момент EventLoop’а вызываются обработчики MessageChannel и я был удивлён. Первая мысль по этому поводу это, что обработчики складываются в микро таски, вперемешку с промисами, но не все так просто. </summary><content type="html">
  &lt;p id=&quot;zr2l&quot;&gt;На днях у меня возник вопрос, а в какой момент EventLoop’а вызываются обработчики MessageChannel и я был удивлён. Первая мысль по этому поводу это, что обработчики складываются в микро таски, вперемешку с промисами, но не все так просто. &lt;/p&gt;
  &lt;p id=&quot;DUFm&quot;&gt;Напомню как выглядит MessageChannel (&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel&quot; target=&quot;_blank&quot;&gt;https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel&lt;/a&gt;)&lt;/p&gt;
  &lt;p id=&quot;fzge&quot;&gt;&lt;code&gt;const channel = new MessageChannel();&lt;/code&gt;&lt;/p&gt;
  &lt;p id=&quot;Mx3d&quot;&gt;&lt;code&gt;channel.port2.postMessage(undefined)&lt;/code&gt;&lt;/p&gt;
  &lt;p id=&quot;Pv0A&quot;&gt;&lt;code&gt;channel.port1.onmessage = () =&amp;gt; {&lt;/code&gt;&lt;/p&gt;
  &lt;p id=&quot;pElh&quot;&gt;&lt;code&gt;console.log(‘massageChannel’);&lt;/code&gt;&lt;/p&gt;
  &lt;p id=&quot;PT5A&quot;&gt;&lt;code&gt;};&lt;/code&gt;&lt;/p&gt;
  &lt;p id=&quot;TKyC&quot;&gt;Самый частый кейс его использования это организация связи между нашим приложением и iframe, shared workers, web workers и т.д.&lt;/p&gt;
  &lt;p id=&quot;lWwp&quot;&gt;Тут пример в какой момент они отрабатывают в рамках EventLoop&lt;br /&gt;&lt;a href=&quot;https://codepen.io/dmitriypereverza/pen/QWQaMrB&quot; target=&quot;_blank&quot;&gt;https://codepen.io/dmitriypereverza/pen/QWQaMrB&lt;/a&gt;&lt;/p&gt;
  &lt;p id=&quot;wnf7&quot;&gt;Как видим вызовы MessageChannel вызываются после всех проминов, но до setTimeout. То есть они лежат и не в очереди тасков и не в очереди микро тасков. А где же тогда?&lt;/p&gt;
  &lt;p id=&quot;pF1I&quot;&gt;А для того чтобы объяснить это обратимся к спецификации и сразу станет все понятно&lt;br /&gt;&lt;a href=&quot;https://html.spec.whatwg.org/multipage/web-messaging.html#:~:text=Each%20event%20loop%20has%20a%20task%20source%20called%20the%20unshipped%20port%20message%20queue&quot; target=&quot;_blank&quot;&gt;https://html.spec.whatwg.org/multipage/web-messaging.html#: ~: text=Each%20event%20loop%20has%20a%20task%20source%20called%20the%20unshipped%20port%20message%20queue&lt;/a&gt;.&lt;/p&gt;
  &lt;p id=&quot;9vO0&quot;&gt;Each event loop has a task source called the unshipped port message queue.&lt;/p&gt;
  &lt;p id=&quot;4QEA&quot;&gt;Теперь вы знаете что есть ещё одна очередь внутри EventLoop. Можно использовать этот козырь на собесах)&lt;/p&gt;

</content></entry></feed>