Last active
December 22, 2017 16:36
-
-
Save allenmichael/5769f7b62800a58b2b19f449dd4ab606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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