Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
SyntaxError: Unexpected token "new" when using await new Promise in JavaScript

I have the following code (snippet out of a larger program and within an async function):

foo = questionData.media ? (await new Promise(resolve => {
  const image = new Image();
  image.onload = function () {
    const canvas = document.ceateElement("canvas");
    canvas.width = this.width;
    canvas.height = this.height;
                    
    canvas.drawImage(image, 0, 0, this.width, this.height);
                    
    resolve(canvas.toDataURL("image/jpg"));
  };
                
  image.src = "https://example.com/" + questionData.media;
})) : false;

I keep getting SyntaxError: Unexpected token 'new' on the first line, and I have no idea why. When I try to use parenthesis:

foo = questionData.media ? (await (new Promise(resolve => {
  const image = new Image();
  image.onload = function () {
    const canvas = document.ceateElement("canvas");
    canvas.width = this.width;
    canvas.height = this.height;
                    
    canvas.drawImage(image, 0, 0, this.width, this.height);
                    
    resolve(canvas.toDataURL("image/jpg"));
  };
                
  image.src = "https://example.com/" + questionData.media;
}))) : false;

And I get Uncaught ReferenceError: await is not defined.

Meanwhile, I have this code that works just fine:

data.push(await new Promise(resolve => {
  const req = new XMLHttpRequest();
  req.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
      resolve(this.responseText);
    }
  };
  req.open("GET", `https://example.com/${id}/answer`, true);
  req.send();
}));

What am I missing?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It sounds like you aren't in an async function, so await does not get interpreted as a keyword, but as a variable name. If you put the entire initial snippet into an async function, it'll work as expected.

What's going on with

Unexpected token 'new'

and

await is not defined

is that, when you're not in an async function, those words are interpreted as normal identifiers (variable names) - if you switch them out for another placeholder variable name, it makes more sense for why the syntax would be invalid:

foo = questionData.media ? (someVariable new Promise(resolve => {
foo = questionData.media ? (someVariable (new Promise(resolve => {

Neither of those makes sense, and so the parser will give you errors as expected, when you use await when not in an async function.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda