title: How can I call a function after grecaptcha.execute() has finished executing - triggered by an event?
body: <p>Currently <code>grecaptcha.execute</code> is being executed on page load as in the first JS example below. If reCAPTCHA challenge is triggered this happens when the page has loaded. Ideally this would happen when the form submit button is clicked instead.
  So I've tried this by moving this into the submit event (second JS example) and put the axios function into a promise. It's submitting before grecaptcha.execute has finished executing. </p>
  
  <p>What is it that I'm not understanding here?
  My first experience with promises so am I not understanding how promises work? Is that not the best solution for this problem? Is is something else entirely?</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;head&gt;
  &lt;script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&amp;render=explicit" defer&gt;&lt;/script&gt;
  &lt;/head&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>const form = document.querySelector('#subscribe');
  let recaptchaToken;
  const recaptchaExecute = (token) =&gt; {
      recaptchaToken = token;
  };
  
  
  const onloadCallback = () =&gt; {
      grecaptcha.render('recaptcha', {
          'sitekey': 'abcexamplesitekey',
          'callback': recaptchaExecute,
          'size': 'invisible',
      });
      grecaptcha.execute();
  };
  
      form.addEventListener('submit', (e) =&gt; {
          e.preventDefault();
          const formResponse = document.querySelector('.js-form__error-message');
          axios({
              method: 'POST',
              url: '/actions/newsletter/verifyRecaptcha',
              data: qs.stringify({
                  recaptcha: recaptchaToken,
                  [window.csrfTokenName]: window.csrfTokenValue,
  
              }),
              config: {
                  headers: {
                      'Content-Type': 'application/x-www-form-urlencoded',
                  },
              },
          }).then((data) =&gt; {
              if (data &amp;&amp; data.data.success) {
                  formResponse.innerHTML = '';
                  form.submit();
              } else {
                  formResponse.innerHTML = 'Form submission failed, please try again';
              }
          });
      }
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>const onloadCallback = () =&gt; {
      grecaptcha.render('recaptcha', {
          'sitekey': 'abcexamplesitekey',
          'callback': recaptchaExecute,
          'size': 'invisible',
      });
  };
  
  form.addEventListener('submit', (e) =&gt; {
      e.preventDefault();
      const formResponse = document.querySelector('.js-form__error-message');
      grecaptcha.execute().then(axios({
          method: 'POST',
          url: '/actions/newsletter/verifyRecaptcha',
          data: qs.stringify({
              recaptcha: recaptchaToken,
              [window.csrfTokenName]: window.csrfTokenValue,
          }),
          config: {
              headers: {
                  'Content-Type': 'application/x-www-form-urlencoded',
              },
          },
      })).then((data) =&gt; {
          if (data &amp;&amp; data.data.success) {
              formResponse.innerHTML = '';
              form.submit();
          } else {
              formResponse.innerHTML = 'Form submission failed, please try again';
          }
      });
  }
  </code></pre>