Is the execution of setTimeout(fnc, 0) called from the constructor() ensured to happen after angular lifecycle hooks ngOnInit & ngAfterViewInit?

I have the following Angular2 ‘pseudo’ code and I want to keep the user class as simple as possible:

// ###### LIB CLASSES ######

@Component()
export abstract class Parent {
  abstract childProvidedFactory;
  factory_config;
  
  ngOnInit() {
    this.dataSource = new FactoryUser(this.childProvidedFactory, this.factory_config);
  }

  // this.dataSource initialized and may be used further
}

export class FactoryUser {
  constructor (childProvidedFactory, factory_config) {
  }

  // ...
}



// ###### USER CLASS ######

@Component()
export class Child extends Parent {
  @Input() config;

  childProvidedFactory = () => {
    // implementation
  }

  ngOnInit() {
    this.factory_config = fnc(this.config);
    super.ngOnInit();
  }
}

I want the user who writes Child not to need to call super.ngOnInit(). So the idea is to move Parent‘s ngOnInit contents to the constructor. However at the constructor execution time both the needed childProvidedFactory and factory_config are not yet available. Therefore I wrap the code with setTimeout:

export abstract class Parent {
  abstract childProvidedFactory;
  factory_config;
  
  constructor() {
    setTimeout(() => {
      this.dataSource = new FactoryUser(this.childProvidedFactory, this.factory_config);
    }, 0)
  }

  // this.dataSource initialized and may be used further
}

Now I may remove super.ngOnInit() from the Child class.

On Chromium, when instrumented with console.logs I get:

Inside child's ngOnInit()
Inside child's ngAfterViewInit()
Inside parent's setTimeout() within constructor()

Is it ensured that setTimeout within Parent‘s constructor will execute after Child‘s ngOnInit repectively after Child‘s ngAfterViewInit lifecycle hooks? If so could you explain why, i.e. how are the lifecycle hooks “scheduled” in the event loop in relation to setTimeout?

The code will be run with the major web engines only (WebKit, Blink, Gecko) and Angular >= 17.

Do you see any other potential issues?


The point is to avoid call of super.ngOnInit() in the Child. So there were other potential solutions:

In this solution I don’t like the Child would need to use special name instead of ngOnInit.

The closest to my question is this, however there the constructor is not added to the mix.

I considered using one-time-initialization with afterNextRender() being called in the constructor. It technically works, however runtime error appears Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError as the variable being initialized in the callback fn is used in the Childs template. So instead of undefined it actually gains a value. With this option there’d be need to inject ChangeDetectorRef to the abstract class’ constructor, so the Child would actually need to pass it to that constructor if I’m not mistaken. This would again unnecessarily complicate the Child‘s code.

New contributor

jan dolezal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật