Skip to content
Browse files

refactor async route match

  • Loading branch information...
1 parent 4857bff commit 0db0d3fa561ce68d2a1ac405029d0fa53a993089 @cloudhead committed Jan 31, 2011
Showing with 21 additions and 33 deletions.
  1. +21 −33 lib/journey.js
View
54 lib/journey.js
@@ -317,17 +317,27 @@ journey.Router.prototype = {
//
// Return the first matching route
//
- asyncFind(this.routes, validateRoute, function (err, found) {
- if (err) {
- dispatcher(err);
- }
- else if (found) {
- dispatcher(null, found);
- } else if (allowedMethods.length) {
- dispatcher(new(MethodNotAllowed)(allowedMethods.join(',')));
- } else {
- dispatcher(null, false);
- }
+ (function find(routes, callback) {
+ var route = routes.shift();
+ if (route) {
+ validateRoute(route, function (err, found) {
+ if (err) { callback(err) }
+ else if (found) { callback(null, found) }
+ else { find(routes, callback) }
+ });
+ } else { // End
+ callback(null, false);
+ }
+ })(this.routes.slice(0), function (err, found) {
+ if (err) {
+ dispatcher(err);
+ } else if (found) {
+ dispatcher(null, found);
+ } else if (allowedMethods.length) {
+ dispatcher(new(MethodNotAllowed)(allowedMethods.join(',')));
+ } else {
+ dispatcher(null, false);
+ }
});
},
@@ -503,25 +513,3 @@ function mixin(target) {
return target;
}
-// Asynchronously iterate over the array, executing iterator
-// passing the result to callback if it is not false
-function asyncFind(array, iterator, cb) {
- var completed = 0;
-
- function iterate() {
- iterator(array[completed], function (err, found) {
- if (err) return cb(err);
- else if (found !== false) {
- return cb(null, found);
- }
-
- completed += 1;
- if (completed === array.length) {
- return cb(null, false);
- }
- iterate();
- });
- }
-
- iterate();
-}

0 comments on commit 0db0d3f

Please sign in to comment.
Something went wrong with that request. Please try again.