Skip to content

Unimplemented features

Rand McKinney edited this page May 2, 2014 · 1 revision

NOTE: Content here moved from main README until the feature is implemented.

Co-style generators

You can obtain the result value of a zone by yielding it.

try {
  var result = yield new Zone(function MyZone() {
    // Do whatever
  });

  // If we get here it all worked.
} catch (err) {
  // The zone failed.
}

Instead of passing a normal function as the zone "body", you can pass a generator constructor. This lets you use the yield keyword within the zone body. Other than that, the zone behaves as if a normal function were passed.

new Zone(function* MyZone() {
  var stats = yield fs.stat('/foo/bar');
});

Zone vs try...catch

When using co-style generators, the ordinary try..catch statement becomes a lot more useful, and its purpose overlaps with that of zones. But there are also differences:

try {
  var connection = net.connect('http://invalid.url'); // no yield
  var fd = yield fs.open('/file/that/exists');
  var fd2 = yield fs.open('/file/that/does/not/exist');
} catch (err) {
  // * The invalid url doesn't cause an error here because the promise isn't
  //   yield-ed.
  // * The attempt to open a nonexisting file failed and got us here.
  // * The file that was successfully opened is still open.
}

Contrast this to:

new Zone(function*() {
  var connection = net.connect('http://invalid.url');
  var fd = yield fs.open('/file/that/exists');
  var fd2 = yield fs.open('/file/that/does/not/exist');
}).catch((err) => {
  // * Either the failed connection attempt, or the nonexisting file
  //  (whichever happened first) made the zone fail.
  // * The zone has automatically closed the file that was succesfully
  //   opened before invoking the catch handler.
});
Clone this wiki locally