Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!