Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional Promises Pattern #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions exercises/async_loops/solution/solution.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
function loadUsers(userIds, load, done) {
var completed = 0
var users = []
userIds.forEach(function(id, index) {
load(id, function(user) {
users[index] = user
if (++completed === userIds.length) return done(users)
})
})
}
const Promise = require('bluebird');

module.exports = loadUsers
module.exports = function loadUsers(userIds, load, done) {
load = Promise.promisify(load); // could be eliminated
return Promise.resolve(userIds)
.map(id => load(id))
.then(done);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By calling done with the Promise chain's native .then get a more backwards compatible pattern as a bonus.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}