title: Async/Await answer onMessage event in cross-domain communication with an iframe
body: <p>I have an iframe, which is communicating with its parent window to set and get some essential cookies via postMessage <a href="https://gist.github.com/buren/8d7c831822bc474164cc37dd522c2d1d" rel="nofollow noreferrer">method</a>.</p>
  
  <p>At first a function in the iframe application requests Cookie A from the parent window.</p>
  
  <pre><code>    function requestCommunication(topic, customerId) {
  
              function cookieAvailable() {
                  return new Promise((resolve) =&gt; resolve(getCookieData('cookieName'));
                  });
              }
  
              console.log(cookieAvailable());
  
              if(!!cookieAvailable()) {
                  //doStuff        
              }
       }
  </code></pre>
  
  <p><code>cookieAvailable()</code> triggers the message from the iframe to the parent.window. In turn, the window returns the cookie with its data as string. This gets done by using the following:</p>
  
  <pre><code> async function getCookieData(cookieName) {
  
          const {data} = await new Promise(resolve =&gt; {
  
              window.onmessage = (event) =&gt; {
                  resolve(event);
              }
          });
  
          var cookieContent = JSON.parse(data);
          var cookieContentData = JSON.parse(cookieContent.data);
  
          return cookieContentData; //this returns the cookie data (in almost all cases)
      }    
  </code></pre>
  
  <p>I don't get how to use the promise correctly to hand it over to my initial trigger function. I would appreciate any support.</p>