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

88
Visualizações
Understanding javascript Promise

[Edit: this is a question about Promises, not setTimeout. Please don't close it without actually reading the question]

I'm trying to understand Promises. From what I've read and understood (which is obviously incomplete or wrong), the following should wait 3 seconds, append "in foo", then append "in bar".

However, "in bar" is first, followed by a 3 second wait, followed by "in foo".

I could use a shove in the right direction on this.

function promiseTest() {
  let promise = new Promise(function(resolve, reject) {
    setTimeout(foo, 3000);
  });
  promise.then(foo).then(bar());
}

function foo() {
  jQuery('#demo').append("<li>in foo</li>");
}

function bar() {
  jQuery('#demo').append("<li>in bar</li>");
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <ul id='demo'></ul>
  <button onclick='promiseTest()'>click</button>
</body>

</html>

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

0

Since this was reopened, and there multiple issues outside of the initial source of confusion, let me try to explain the problems.

1. Syntax for passing a callback

Executing a callback function is the responsibility of the function you pass it to. So in the case of setTimeout, you expect that after 3 seconds it will call the function you pass it.

const foo = () => console.log('foo');
setTimeout(foo, 3000);
// OR
setTimeout(() => console.log('foo'), 3000);

In both scenarios, you are passing a function, but never calling it yourself.

promise.then(foo).then(bar()) is a problem because you are calling bar, not passing it.

2. Unresolved promises

promise never completes, because you never called resolve or reject. This means, neither .then actually executes.

You were deceived into thinking they were because of the bug with bar().

Final solution

In order to accomplish what you want, you need to resolve the promise. But you need to do so after foo is called. The best way to do this may be to create an anonymous function for the setTimeout callback that calls both foo and resolve.

See below:

function promiseTest() {
  let promise = new Promise(function(resolve, reject) {
    setTimeout(function() {
      foo();
      resolve();
    }, 3000);
  });
  promise.then(bar);
}

function foo() {
  jQuery('#demo').append("<li>in foo</li>");
}

function bar() {
  jQuery('#demo').append("<li>in bar</li>");
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
  <ul id='demo'></ul>
  <button onclick='promiseTest()'>click</button>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório

0

The argument you're supposed to pass to then() needs to be a function, you are not passing the function bar, you are passing the result of calling the function bar.

Remove the parenthesis to refer to the function instead of the result of calling it.

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