Created
January 5, 2016 05:09
-
-
Save purplejacket/07fd1a7823fd96971887 to your computer and use it in GitHub Desktop.
Wrapping multiple calls to directory.getAccounts into a Promise
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
| var getAccounts = function(directoryHref, usernames, callback) { | |
| client.getDirectory(directoryHref, function(err, directory) { | |
| var accountPromises = _.map(usernames, function(username) { | |
| return new Promise(function(resolve, reject) { | |
| var foundAccount; | |
| directory.getAccounts({username: username, expand: 'customData'}, | |
| function(err, accounts) { | |
| accounts.each( | |
| function(account, cb) { | |
| account.id = account.username; | |
| foundAccount = account ; | |
| cb(); | |
| }, | |
| function(err) { | |
| resolve(foundAccount); | |
| } | |
| ); | |
| } | |
| ); | |
| }); | |
| }); | |
| Promise.all(accountPromises).then(function(accounts) { | |
| callback(_.compact(accounts)); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment