Javascript script returning SyntaxError: missing; before statement only in firefox

0

I have a javascript function to read files that are inside the server. In all browsers it works perfectly, except for 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;
}
    
asked by anonymous 30.10.2018 / 19:43

1 answer

1

There is no error in the posted code, but an outdated browser will acknowledge errors because the async and await keywords were only introduced in ES2017, so the browser will not be able to run this code.

I'll take the time to quote: you're misusing 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 get back what you got. But what if the response takes more than half a second to return? You could very well use

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