Federico Grandi
Federico Grandi

Reputation: 6796

Promise.then() executing before the promise is resolved

I'm having a problem with promises. I have this code:

let line = hey.fetchBans().then(() => {
  myStuff();
  console.log(line);
});

The problem is that myStuff() executes before fetchBans() is resolved. I tried to see if that was the real problem logging line, and it prints Promise { <pending> }

What am I doing wrong?

Upvotes: 2

Views: 1405

Answers (1)

Bergi
Bergi

Reputation: 665286

line is not the promise that hey.fetchBans() returned (and which did fulfill before your callback is called), it is the promise that .then(…) returned (and which will be resolved with the result value of the callback). It will always be pending inside that very callback.

Upvotes: 4

Related Questions