Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow user to specify concurrency for per_page.

  • Loading branch information...
commit 42b6dbdec71b35e64caf9a4bd60e14a5db7ffdb1 1 parent d51e2db
@chjj authored
Showing with 41 additions and 7 deletions.
  1. +41 −7 lib/charged.js
View
48 lib/charged.js
@@ -944,32 +944,66 @@ Charged.prototype.get = function(path, query, callback, format) {
if (query && query.per_page > 50 && query.page == null) {
var max = query.per_page
- , out = [];
+ , out = []
+ , concurrency
+ , pending = 0;
+
+ if (query.concurrency) {
+ concurrency = query.concurrency;
+ delete query.concurrency;
+ } else {
+ concurrency = 1;
+ }
query.per_page = 200;
query.page = 0;
- (function next() {
+ var cb = function(err) {
+ // If `out` is maxed, execute callback even
+ // if there are still requests pending.
+ if (pending > 0 && out.length < max) return;
+
+ if (cb.done) return;
+ cb.done = true;
+
+ return err
+ ? callback(err)
+ : callback(null, out);
+ };
+
+ var page = function() {
query.page++;
+ pending++;
return self.get(path, query, function(err, results) {
- if (err) return callback(err);
+ pending--;
+
+ if (err) return cb(err);
if (!results.length) {
- return callback(null, out);
+ return cb();
}
out.push.apply(out, results);
if (out.length >= max) {
out = out.slice(0, max);
- return callback(null, out);
+ return cb();
}
return next();
}, format);
- })();
+ };
+
+ var next = function() {
+ var i = concurrency;
+ while (i--) page();
+ };
+
+ return next();
+ }
- return;
+ if (query && query.concurrency) {
+ delete query.concurrency;
}
if (query && query._per_page) {
Please sign in to comment.
Something went wrong with that request. Please try again.