Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a ZoneAwarePromise

I am using angular 6. One of the http calls returns a ZoneAwarePromise when I try to convert the Observable into a Promise. Also the then block is not getting called.

const login = this.authService.login(email, password).toPromise()
login.then(() => {\* not getting called*\})

Can someone explain what is a ZoneAwarePromise ?

like image 863
Muthukumar Avatar asked Sep 18 '18 17:09

Muthukumar


1 Answers

Angular relies heavily on zone.js to persist an execution context across asynchronous tasks. It's wrapped up in an injectable service called NgZone.

These Zones wrap up common JS objects meant to run async tasks, which include Promises. This is maintained in a Zone as a Task, MicroTask, etc.

A ZoneAwarePromise is still functionally identical to a normal Promise, but internally stays aware of a Zone's execution context, and the Zone can know when that Promise completes.

In Angular, this execution context almost always means running change detection.

like image 181
joh04667 Avatar answered Sep 20 '22 12:09

joh04667