Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Last active December 22, 2017 16:36
Show Gist options
  • Save allenmichael/5769f7b62800a58b2b19f449dd4ab606 to your computer and use it in GitHub Desktop.
Save allenmichael/5769f7b62800a58b2b19f449dd4ab606 to your computer and use it in GitHub Desktop.
'use strict';
const box = require('box-node-sdk');
const fs = require('fs');
const util = require('util');
let configFile = fs.readFileSync('config.json');
configFile = JSON.parse(configFile);
let session = box.getPreconfiguredInstance(configFile);
let serviceAccountClient = session.getAppAuthClient("enterprise");
serviceAccountClient._useIterators = true;
serviceAccountClient.enterprise.getUsers({ limit: 1000 })
.then((usersIterator) => {
return autoPage(usersIterator);
})
.then((usersCollection) => {
console.log(usersCollection.length);
});
function autoPage(iterator) {
let collection = [];
let moveToNextItem = () => {
return iterator.next()
.then((item) => {
if (item.value) {
collection.push(item.value);
}
if (item.done !== true) {
return moveToNextItem();
} else {
return collection;
}
})
}
return moveToNextItem();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment