[InterMine Dev] question about programming with promises

Alex Kalderimis alex at intermine.org
Wed Mar 26 13:03:37 GMT 2014


Hi there Joel,

There are various libraries that manage control flow around promises
(sequencing, parallelism, etc), of which I can recommend Q
(https://github.com/kriskowal/q)

In this case you want to make sure that all promises are fulfilled
before any call to log, so execution in parallel is fine (here I have
extracted all the inline functions just to make them more readable):

 /* Take a path-string, return a promise for a name. */
 var promiseName = function (p) {
   return intermine.PathInfo.parse(p).getDisplayName();
 };
 /* Error handler */
 var onError = function (err) {
   console.error(err);
 };
 /* Utility for building a promise of an array of values
  * from an array of promises */
 var buildArray = function (xs, p) {
   return p.then(function (x) { return xs.concat([x]); });
 };
 /* Predicate fn for editable constraints */
 var isEditable = function (c) {
   return c.editable;
 };
 /* Take a list of things, and log them one-by-one */
 var logAll = function (xs) {
   xs.forEach(function (x) {
     console.log(x);
   });
 };

 template.constraints
   .filter(isEditable)     // Only editable constraints.
   .map(promiseName)       // As array of promises for names
   .reduce(buildArray, []) // As a single promise for a list of names
   .then(logAll, onError); // print them all out

This is essentially what is going on behind the scenes when you do:

 Q.all(template.constraints.filter(isEditable).map(promiseName))
  .then(logAll, onError); // print them all out

All best,

Alex


On 26/03/14 12:20, Joel Richardson wrote:
> 
> Hi Alex,
> 
> I'm confused about how to do something using the imjs API. It seems
> maddeningly simple but I just can't wrap my head around it. At the heart
> of the issue (I think) is my confusion about promises. How do you
> iterate, when each step involves an asynchronous call?  Specific example
> (nonsensical, but illustrates the point): concatenate the display names
> of the editable constraints in a template. Normally, this would be a
> simple loop.
> 
>   var ans = "";
>   tmplt.constraints.forEach(function(c){
>     ans += c.editable ?
> intermine.PathInfo.parse(c.path).getDisplayName() : "";
>   });
>   console.log(ans);
>   
> 
> But in this case, the critical call (getDisplayName()) returns a promise
> rather than the actual string. So how do I ensure that (a) all the
> promises are fulfilled before calling console.log, and (b) are fulfilled
> in the right order, so that the answer is the deterministic?
> 
> Thanks,
> Joel
> -- 
> Joel E. Richardson, Ph.D.
> Sr. Research Scientist
> Mouse Genome Informatics
> The Jackson Laboratory
> 600 Main Street
> Bar Harbor, Maine 04609
> 207-288-6435
> joel.richardson at jax.org
> 
> The information in this email, including attachments, may be
> confidential and is intended solely for the addressee(s). If you believe
> you received this email by mistake, please notify the sender by return
> email as soon as possible.
> 



More information about the dev mailing list