Created
January 30, 2015 06:38
-
-
Save TGOlson/d915ccd9ec9d9a41d657 to your computer and use it in GitHub Desktop.
Map and resolve all
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 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