Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created January 30, 2015 06:38
Show Gist options
  • Select an option

  • Save TGOlson/d915ccd9ec9d9a41d657 to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/d915ccd9ec9d9a41d657 to your computer and use it in GitHub Desktop.
Map and resolve all
var Q = require('q'),
_ = require('lodash');
var mapAndResolveAll = _.compose(Q.all, _.map);
function loadWithPattern(pattern) {
return function(entity) {
return loadEntity(entity, pattern);
};
}
function loadEntity(entity, pattern) {
return entity.load(pattern);
}
var log = console.log.bind(console),
// array of entities
entities = [entity, entity, entity],
// dummy load pattern
pattern = {
type: true
};
mapAndResolveAll(entities, loadWithPattern(pattern)).then(log);
// wait 500 ms
// => [ { load: [Function], type: ... },
// { load: [Function], type: ... },
// { load: [Function], type: ... } ]
// equivalent to:
// var promises = _.map(entities, function(entity) {
// return entity.load(pattern);
// });
// Q.all(promises).then(function(result) {
// console.log(result);
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment