Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何捕获Google recaptchaV3承诺超时?_Javascript_Reactjs_Typescript_Recaptcha V3 - Fatal编程技术网

Javascript 如何捕获Google recaptchaV3承诺超时?

Javascript 如何捕获Google recaptchaV3承诺超时?,javascript,reactjs,typescript,recaptcha-v3,Javascript,Reactjs,Typescript,Recaptcha V3,调用上面的wait函数时,我曾从recaptcha获取超时控制台错误(发现这是因为组件呈现导致badge元素被删除),但为了避免它,我如何捕获它并解析返回的空字符串 错误如下所示: 既然承诺中有错误,您是否尝试过.catch()它 import canUseDOM from '@utils/dist/env/canUseDOM'; declare global { interface Window { grecaptcha: any; } } export default a

调用上面的wait函数时,我曾从recaptcha获取超时控制台错误(发现这是因为组件呈现导致badge元素被删除),但为了避免它,我如何捕获它并解析返回的空字符串

错误如下所示:
既然承诺中有错误,您是否尝试过
.catch()

import canUseDOM from '@utils/dist/env/canUseDOM';

declare global {
  interface Window {
    grecaptcha: any;
  }
}

export default async function getRecaptchaTokenExplicit(params: { recaptchaClientId: number }) {
  return new Promise(resolve => {
    if (canUseDOM && window.grecaptcha) {
      const { recaptchaClientId } = params;

      window.grecaptcha.ready(() => {
        window.grecaptcha
          .execute(recaptchaClientId, {
            action: 'submit',
          })
          .then(function(token: string) {
            return resolve(token);
          });
      });
    } else {
      return resolve('');
    }
  });
}

为什么不使用
try…catch…
?看看它是如何工作的,
try{getRecaptchaTokenExplicit()}catch(e){}
诸如此类,错误超时仍然会出现。我想稍后将Promise.race与setTimeout一起使用,看看是否有效。
window.grecaptcha.execute(recaptchalientid,{action:'submit'})。然后(函数(token:string){return resolve(token)})。catch(err=>resolve('')
window.grecaptcha
    .execute(recaptchaClientId, {
        action: 'submit',
    })
    .then(function(token: string) {
        resolve(token);
    })
    .catch(err => {
        console.error(err);
        resolve('');
    });