简体   繁体   中英

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)?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM