After upgrading from Angular 9 to 10 I get these warnings when I run ng serve:
WARNING: Zone.js does not support native async/await in ES2017.
These blocks are not intercepted by zone.js and will not triggering change detection.
See: https://github.com/angular/zone.js/pull/1140 for more information.
(node:56581) ExperimentalWarning: The fs.promises API is experimental
Anybody know what this means and if it is something I should care about?
js provides a mechanism, called zones, for encapsulating and intercepting asynchronous activities in the browser (e.g. setTimeout , , promises). These zones are execution contexts that allow Angular to track the start and completion of asynchronous activities and perform tasks as required (e.g. change detection).
Browser Support: As of 2020, 94% of browsers worldwide can handle async/await in javascript Notable exceptions are IE11 and Opera Mini.
await blocks the code execution within the async function, of which it ( await statement ) is a part. There can be multiple await statements within a single async function. When using async await , make sure to use try catch for error handling.
I agree with Andrei. It might not update your component, especially if you're getting the data from an API somewhere. To solve this, you could change the target in your tsconfig.base.json
file back to es2015
.
The accepted answer is correct, but to be more specific, per this longstanding issue on GitHub, if you target es2015
, your async
functions will be transpiled to use a helper function called _awaiter
, which uses a generator function under the hood. Because of technical constraints outlined at the above link, it is possible for Zone.js to hook the generator function and manage its execution in its own microtask queue, but it is not possible to do so for a native await
statement.
The net result is that, if your function is executing in the context of Zone A, then you hit an await
statement, when the next line of your function finally executes, it will be in the Root Zone. From that issue:
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const test = async () => {
console.log(Zone.current.name) // will output 'angular'
await delay(100);
console.log(Zone.current.name) // will output 'root'
}
I believe that this also means that any Zone feature that allows you to manage task execution (like the Angular test helpers fakeAsync
/ tick
/ flush
) will fail to correctly recognize the "task" (of waiting for the await
to resolve) as pending. (Which, of course, is why this is a problem in the first place.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With