runAfterChangesObserved method

void runAfterChangesObserved(
  1. void callback()
)

Executes a callback after changes were observed by the zone.

Use this method instead of Future, Timer.run, and scheduleMicrotask to execute a callback after change detection has run:

void example(NgZone zone) {
  someValue = true;
  zone.runAfterChangesObserved(() {
    doSomethingDependentOnSomeValueChanging();
  });
}

Note that unlike Future and Timer.run, this method will execute callback in the current event loop before yielding to the browser. This means that changes made prior to invoking this method, even if reflected in the DOM, may not be visible until after callback returns and the browser can render another frame.

Implementation

void runAfterChangesObserved(void Function() callback) {
  if (isRunning) {
    onTurnDone.first.whenComplete(() => scheduleMicrotask(callback));
  } else {
    scheduleMicrotask(callback);
  }
}