title: window.crypto.subtle.encrypt returns undefined
body: <p>I am trying to encrypt a text using the Web Crypto API. The following is my code:</p>
  
  <pre><code>const generatedIv = generateIv();
  let generatedKey;
  generateKey()
    .then(key =&gt; {
      generatedKey = key;
      encryptData(encodeText(JSON.stringify(data)), generatedKey, generatedIv);
    }).then(encrypted =&gt; {
      console.log(encrypted) // Always returns undefined?
    }).catch(
      err =&gt; console.error(err)
    );
  
  function encodeText(data) {
    if ('TextEncoder' in window) {
      return new TextEncoder('utf-8').encode(data);
    }
    return undefined;
  }
  
  function generateIv() {
    return window.crypto.getRandomValues(new Uint8Array(12));
  }
  
  function generateKey() {
    return window.crypto.subtle.generateKey({
      name: 'AES-GCM',
      length: 256
    }, true, [
      'encrypt',
      'decrypt'
    ]);
  }
  
  function encryptData(data, key, iv) {
    return window.crypto.subtle.encrypt({
      name: 'AES-GCM',
      iv: iv,
      tagLength: 128
    }, key, data);
  }
  </code></pre>
  
  <p>For some reason, the <code>console.log</code> statement always prints out <code>undefined</code>. I've tried looking on the web for possible solutions, but I wasn't able to find any.</p>
  
  <p>Is there something that I am doing wrong here? Thanks in advance! :)</p>