Implements Zones for JavaScript
trevorade and mhevery Fix ZoneAwarePromise.all to resolve at the correct time (#1150)
For ZoneAwarePromise.all, as [implemented](https://github.com/angular/zone.js/blob/7201d44451f6c67eac2b86eca3cbfafde14035d6/lib/common/promise.ts), the `count` variable serves three purposes:

1.  Count the number of values passed-in
2.  Specify the index of a resolved value in `resolvedValues`
3.  Track when all the promises have been resolved.

In the event that `value.then` is immediately called, `count` will be decremented at the incorrect time resulting in a potentially negative value or reaching 0 when not all values have actually been resolved. 

The fix is to be more meticulous about using the correct indices at the correct time and to not overload the count and number of resolved promises.  This updated version is more explicit about the purpose of the `unresolvedCount` and `valueIndex` variables.  `unresolvedCount` needs to start at 2 to prevent `resolve` from being called too soon in the event that `value.then` calls the callback immediately. The scoping of the index for use in `resolvedValues` is made constant to guarantee the correct index is used.

This buggy behavior specifically manifested as an issue in the Monaco editor but most likely occurs elsewhere as well in cases where promises may immediately call into `.then`.

Related issue:
Microsoft/monaco-editor#790 (comment)
Latest commit 9ed5712 Oct 26, 2018
Permalink
Failed to load latest commit information.
dist chare: release v0.8.26 Apr 8, 2018
doc chore: Fix the URL to zone.js.d.ts (#922) Dec 27, 2017
example feat(eventListener): fix #798, improve EventTarget.addEventListener p… Jun 30, 2017
lib Fix ZoneAwarePromise.all to resolve at the correct time (#1150) Oct 26, 2018
scripts chore: refactor closure build (#844) Jul 20, 2017
test feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
.clang-format build: add a clang-format task Oct 4, 2016
.editorconfig chore: add support for .editorconfig Apr 21, 2017
.gitignore chore: update .gitignore with build-esm Sep 10, 2016
.travis.yml feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
CHANGELOG.md chare: release v0.8.26 Apr 8, 2018
DEVELOPER.md feat(build): use yarn instead of npm (#1025) Feb 27, 2018
LICENSE style(License): update copyright year (#986) Feb 8, 2018
LICENSE.wrapped chore(everything): Major Zone Rewrite/Reimplementation Feb 29, 2016
MODULE.md feat(error): fix #975, can config how to load blacklist zone stack fr… Jun 24, 2018
NON-STANDARD-APIS.md feat(patch): fix #1011, patch ResizeObserver (#1012) Feb 12, 2018
README.md doc(sample): add samples to stackblitz (#1059) Jun 18, 2018
SAMPLE.md doc(sample): add samples to stackblitz (#1059) Jun 18, 2018
STANDARD-APIS.md fix(event): blackListedEvents also apply on addEventListener (#968) Dec 27, 2017
check-file-size.js fix(core): add file check script in travis build Jan 10, 2018
file-size-limit.json feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
gulpfile.js feat(error): fix #975, can config how to load blacklist zone stack fr… Jun 24, 2018
karma-base.conf.js feat(build): Upgrade to TypeScript 2.9 and rxjs6 (#1122) Sep 6, 2018
karma-build-jasmine-phantomjs.conf.js fix(websocket): fix #824, patch websocket onproperties correctly in … Jul 12, 2017
karma-build-jasmine.conf.js fix: Run tests on both the build as well as the dist folder (#514) Dec 18, 2016
karma-build-jasmine.es6.conf.js feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
karma-build-mocha.conf.js fix: Run tests on both the build as well as the dist folder (#514) Dec 18, 2016
karma-build-sauce-mocha.conf.js fix: Run tests on both the build as well as the dist folder (#514) Dec 18, 2016
karma-build-sauce-selenium3-mocha.conf.js fix(test): fix mocha compatible issue (#1028) Mar 14, 2018
karma-build.conf.js feat(error): fix #975, can config how to load blacklist zone stack fr… Jun 24, 2018
karma-dist-jasmine.conf.js fix: Run tests on both the build as well as the dist folder (#514) Dec 18, 2016
karma-dist-mocha.conf.js fix(test): karma-dist should test bundle under dist (#1049) Jun 18, 2018
karma-dist-sauce-jasmine.conf.js fix(promise): can set native promise after loading zone.js (#899) Sep 15, 2017
karma-dist-sauce-jasmine.es6.conf.js feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
karma-dist-sauce-jasmine3.conf.js fix(jasmine): fix #1015, make jasmine patch compatible to jasmine 3.x ( Feb 17, 2018
karma-dist-sauce-selenium3-jasmine.conf.js ci(SauceLabs): refresh browsers list (#716) Apr 10, 2017
karma-dist.conf.js fix(test): karma-dist should test bundle under dist (#1049) Jun 18, 2018
package.json feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
presentation.png readme: add ng-conf presentation Jan 27, 2014
promise-adapter.js feat(promise): fix #621, add unhandledRejection handler and ignore co… Feb 8, 2017
promise.finally.spec.js feat(promise): support Promise.prototype.finally (#1005) Feb 10, 2018
sauce-selenium3.conf.js feat(error): fix #975, can config how to load blacklist zone stack fr… Jun 24, 2018
sauce.conf.js feat(error): fix #975, can config how to load blacklist zone stack fr… Jun 24, 2018
sauce.es6.conf.js feat(custom-element): patch customElement v1 APIs (#1133) Sep 11, 2018
simple-server.js feat(saucelab): integrate saucelab webdriver test. (#819) Jul 6, 2017
tsconfig-esm-node.json fix(tsc): tsconfig.json strict:true Jun 18, 2018
tsconfig-esm.json fix(tsc): tsconfig.json strict:true Jun 18, 2018
tsconfig-node.json fix(tsc): tsconfig.json strict:true Jun 18, 2018
tsconfig.json fix(tsc): tsconfig.json strict:true Jun 18, 2018
tslint.json build: enforce no var keyword Dec 28, 2016
webpack.config.js chore(everything): Major Zone Rewrite/Reimplementation Feb 29, 2016
yarn.lock feat(core): upgrade to typescript 3.0.3 (#1132) Sep 10, 2018

README.md

Zone.js

Build Status CDNJS

Implements Zones for JavaScript, inspired by Dart.

If you're using zone.js via unpkg (i.e. using https://unpkg.com/zone.js) and you're using any of the following libraries, make sure you import them first

  • 'newrelic' as it patches global.Promise before zone.js does
  • 'async-listener' as it patches global.setTimeout, global.setInterval before zone.js does
  • 'continuation-local-storage' as it uses async-listener

NEW Zone.js POST-v0.6.0

See the new API here.

Read up on Zone Primer.

What's a Zone?

A Zone is an execution context that persists across async tasks. You can think of it as thread-local storage for JavaScript VMs.

See this video from ng-conf 2014 for a detailed explanation:

screenshot of the zone.js presentation and ng-conf 2014

See also

Standard API support

zone.js patched most standard web APIs (such as DOM events, XMLHttpRequest, ...) and nodejs APIs (EventEmitter, fs, ...), for more details, please see STANDARD-APIS.md.

Nonstandard API support

We are adding support to some nonstandard APIs, such as MediaQuery and Notification. Please see NON-STANDARD-APIS.md for more details.

Examples

You can find some samples to describe how to use zone.js in SAMPLE.md.

Modules

zone.js patches the async APIs described above, but those patches will have some overhead. Starting from zone.js v0.8.9, you can choose which web API module you want to patch. For more details, please see MODULE.md.

Promise A+ test passed

Promises/A+ 1.1 compliant

License

MIT