title: How can I get the results from multiple pages in an API response?
body: <p>I want to paginate through all the results for a specific of a ChartMogul Customers.All() API call. I'm using the <a href="https://github.com/chartmogul/chartmogul-node" rel="nofollow noreferrer">Chartmogul NPM package</a>.</p>
  
  <p>It works, I can get all the results, but I have more results than the max number of records allowed in a single call, so I need to page through multiple pages of results. And I'm stuck how to elegantly do it.</p>
  
  <p>I could do multiple calls first grabbing the total amount of pages, followed by a loop that goes through all pages. Saving all results in one big object.</p>
  
  <p>This is the call, with the <code>page</code> variable should get updated to x page. </p>
  
  <pre class="lang-js prettyprint-override"><code>ChartMogul.Customer.all(config, {
      status: 'Active',
      per_page: 200,
      page: page
  }, function (err, res) {
      if (err) reject(err);
      console.log(res);
      resolve(res.entries);
  });
  </code></pre>
  
  <p>Each API response from chartmogul contains the following details</p>
  
  <pre><code>{
    "entries":[
      {
        "id": 25647,
        "uuid": "cus_de305d54-75b4-431b-adb2-eb6b9e546012",
        "external_id": "34916129",
        "external_ids": ["34916129"],
        "data_source_uuid": "ds_610b7a84-c50f-11e6-8aab-97d6db98913a",
        "data_source_uuids": ["ds_610b7a84-c50f-11e6-8aab-97d6db98913a"],
        "name": "Example Company",
        "company": "",
        "email": "bob@examplecompany.com",
        "status": "Active",
        "lead_created_at": "2015-01-01T10:00:00-04:00",
        "free_trial_started_at": "2015-01-09T10:00:00-04:00",
        "customer-since": "2015-06-09T13:16:00-04:00",
        "city": "Nowhereville",
        "state": "Alaska",
        "country": "US",
        "zip": "0185128",
        "attributes":{
          "tags": ["engage", "unit loss", "discountable"],
          "stripe":{
            "uid": 7,
            "coupon": true
          },
          "clearbit":{
            "company":{
              "name": "Example Company",
              "legalName": "Example Company Inc.",
              "domain": "examplecompany.com",
              "url": "http://examplecompany.com",
              "category":{
                "sector": "Information Technology",
                "industryGroup": "Software and Services",
                "industry": "Software",
                "subIndustry": "Application Software"
              },
              "metrics":{
                "raised": 1502450000,
                "employees": 1000,
                "googleRank": 7,
                "alexaGlobalRank": 2319,
                "marketCap": null
              },
            },
            "person":{
              "name":{
                "fullName": "Bob Kramer"
              },
              "employment":{
                "name": "Example Company"
              }
            }
          },
          "custom":{
            "CAC": 213,
            "utmCampaign": "social media 1",
            "convertedAt": "2015-09-08 00:00:00",
            "pro": false,
            "salesRep": "Gabi"
          }
        },
        "address":{
          "address_zip": "0185128",
          "city": "Nowhereville",
          "country": "US",
          "state": "Alaska"
        },
        "mrr": 3000,
        "arr": 36000,
        "billing-system-url": "https:\/\/dashboard.stripe.com\/customers\/cus_4Z2ZpyJFuQ0XMb",
        "chartmogul-url": "https:\/\/app.chartmogul.com\/#customers\/25647-Example_Company",
        "billing-system-type": "Stripe",
        "currency": "USD",
        "currency-sign": "$"
      },
      {"...49 more...": "...entries..."}
    ],
    "has_more": true,
    "per_page": 50,
    "page": 1,
    "current_page": 1,
    "total_pages": 4
  }
  
  </code></pre>
  
  <p>So how can I loop through all pages of a response in a simple, elegant way?</p>