9951 explained code solutions for 126 technologies

angularjsHow can I use Zone.js with Angular to detect and act upon asynchronous events?

Zone.js is a library that can be used in conjunction with Angular to detect asynchronous events. It allows developers to intercept asynchronous operations and execute custom code when certain events occur.

To use Zone.js with Angular, the library must first be imported and initialized. This can be done by importing the Zone.js library into the app.module.ts file and then calling the Zone.js run method within the ngOnInit lifecycle hook, like so:

import { Component, OnInit } from '@angular/core';
import * as Zone from 'zone.js';

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
export class AppComponent implements OnInit {
  ngOnInit() {
    Zone.run(() => {
      // Your custom code here

Once the Zone.js library is initialized, developers can use the Zone.current object to access the current Zone and add custom hooks to detect asynchronous events. For instance, the following code will log a message to the console whenever an asynchronous event occurs:

  name: 'AsyncHook',
  onInvokeTask: (parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) => {
    console.log('Async event detected!');
    return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs);

Once the custom hooks are added, Zone.js will detect asynchronous events and execute the custom code whenever the events occur.

Parts of the code:

  • import * as Zone from 'zone.js'; - imports the Zone.js library
  • Zone.run(() => { ... }); - initializes the Zone.js library
  • Zone.current.fork({ ... }); - creates a new Zone with custom hooks
  • onInvokeTask: (...) => { ... } - custom hook that will be executed when an asynchronous event occurs
  • console.log('Async event detected!'); - logs a message to the console when an asynchronous event occurs

Helpful links

Edit this code on GitHub