Permalink
Browse files

Refactor

  • Loading branch information...
1 parent 7271b5a commit afc3199391682855640e110e9e5254c15ca49fe6 @nakamura-to committed Jul 7, 2012
File renamed without changes.
File renamed without changes.
View
@@ -56,11 +56,15 @@ function Async(count, failFast) {
Async.prototype.await = function await(callback) {
if (this.error) {
- callback(this.error, null, new Gate());
+ next(this.error, null);
} else if (this.pending === 0 && this.count <= 0) {
- callback(null, this.results, new Gate());
+ next(null, this.results);
} else {
- this.next = callback;
+ this.next = next;
+ }
+
+ function next(error, results) {
+ callback(error, results, new Gate());
}
};
@@ -79,31 +83,33 @@ Async.prototype.makeCallback = function makeCallback(caller, name, mapping) {
var next = self.next;
self.pending--;
if (!self.canceled) {
- var isError = error instanceof Error;
- if (isError) {
+
+ if (error instanceof Error) {
if (!('gate_location' in error)) {
+ // expand the error object
error.gate_location = location;
}
- }
- if (isError && self.failFast) {
- self.canceled = true;
- if (next) {
- self.next = noop;
- next(error, null, new Gate());
- } else {
- self.error = error;
+ if (self.failFast) {
+ self.canceled = true;
+ if (next) {
+ self.next = noop;
+ next(error, null);
+ } else {
+ self.error = error;
+ }
+ return;
}
+ }
+
+ var result = mapArguments(mapping, arguments);
+ if (name === null) {
+ self.results[index] = result;
} else {
- var result = mapArguments(mapping, arguments);
- if (name === null) {
- self.results[index] = result;
- } else {
- self.results[name] = result;
- }
- if (self.pending === 0 && self.count <= 0 && next) {
- self.next = noop;
- next(null, self.results, new Gate());
- }
+ self.results[name] = result;
+ }
+ if (self.pending === 0 && self.count <= 0 && next) {
+ self.next = noop;
+ next(null, self.results);
}
}
};
View
@@ -128,7 +128,7 @@ describe('latch', function() {
callback(new Error('ERROR'));
}, 0);
g.await(function (err, results) {
- assert.ok('ERROR', results[0].message);
+ assert.strictEqual('ERROR', results[0].message);
done();
});
});

0 comments on commit afc3199

Please sign in to comment.