Search code examples
angulartypescriptasync-awaitzone

Why async function return 'zone object'


this question is a little related with this one. Here we have following code in ANGULAR:

private async createFloor(name) 
{
    let newFloorData = { 
        floorName: name,
        percent: 0,
        requestSubscription: null,
        finish: false,
        deleted: false,
    };

    ...

    return newFloorData;
}

public async addFloor(event) 
{
     let newFloorData = this.createFloor('test name');
     debugger;
     ...
}

And in debugger chrome debugger when i look on newFloorData I get following information:

ZoneAwarePromise

__zone_symbol__state : true

__zone_symbol__value : {percent: 0, requestSubscription: null, finish: false, deleted: false}

proto : Object

However if I add await in addFloor function:

public async addFloor(event) 
{
     let newFloorData = await this.createFloor('test name');
     debugger;
     ...
}

In debugger I just get newFloorData object returned by createFloor (which is intuitive).

Question: Why? What mechanism is behind this behaviour?


Solution

  • Angular uses zone.js to patch almost all async APIs. It does this to get notified when an async action happened and then triggers a change detection run.

    See also