Javascript Script Returning Syntaxerror: Missing ; before statement only in firefox

Asked

Viewed 37 times

0

I have a javascript function to read files that are inside the server. In all browsers it Funiona perfectly, with the exception of firefox that returns the error:

Syntaxerror: Missing ; before statement

Function code:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
async function lerArquivo(arquivo) {
  var response;
  jQuery.get(arquivo, function(data) {
      response = data;
  }, 'text');
  await sleep(500);
  return response;
}
  • 1

    What would be the line of error?

  • In firefox accuses the first line after the end of the Sleep function, ie the line on which I declare the file function.

1 answer

1


There is no error in the code posted, but an outdated browser will error because the async and await keywords were only entered in ES2017, and therefore the browser will not be able to run that code.

I will take this opportunity to quote: you are making misuse of AJAX in your code. Instead of returning the answer after getting it from the server, you’re waiting half a second and then trying to return what you got. But what if the answer takes more than half a second to return? You might as well use

function lerArquivo(arquivo) {
  return new Promise((resolve, reject) => {
      jQuery.get(arquivo, 'text')
          .done(resolve)
          .fail(reject);
  });
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.