Dmitry Nagiev
Dmitry Nagiev

Reputation: 103

AngularJS promises: if the promise ready before the function returns

I have a function call returning a promise which takes about 10ms to complete I call that function inside(at the beginning of another function) that takes long long time to complete (no asynchronous code it it). If the promise is resolved before its host function completes execution, will it wait for its host function to finish?

function foo() {
  getSomeValues().then(function() {
    // getSomeValues takes approx. 10ms
    console.log('Got Values');
  });

  for (var i = 0; i < 3; i++) {
    // both operations take approx. 900ms
    moveFile();
    moveFileBack();
    console.log('Iteration: ' + (i + 1));
  }
}

What would this function print into console(in what order)?

Upvotes: 1

Views: 67

Answers (1)

SLaks
SLaks

Reputation: 887657

Yes.

Promise callbacks will always run asynchronously on the next event loop frame.

It is completely impossible for code to interrupt your function while it's executing (since Javascript is single-threaded).

Upvotes: 4

Related Questions