Validating country inside form

I have used a code to validate and select country using Javascript.

How to manage without an error as it shows me an error (syntax error): .then((resp) => resp.json())

function getIp(callback) {
 fetch('https://ipinfo.io/json?token=32321432423', {headers: {'Accept': 'application/json'}})
 .then((resp) => resp.json())
 .catch(() =>
  {
   return {
   country: 'us',
   };
  }
 )
 .then((resp) => callback(resp.country));
}

There is no syntax error in the code you posted, so this is probably being generated elsewhere.

FWIW, I would stick the catch at the end:

function getIp(callback) {
  fetch('https://ipinfo.io/json?token=32321432423', { headers: { Accept: 'application/json' } })
    .then((resp) => resp.json())
    .then((resp) => callback(resp.country))
    .catch(() => ({ country: 'us' }));
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.