when.map & passing params

16 views
Skip to first unread message

Paul Tiseo

unread,
Jul 20, 2013, 8:12:43 PM7/20/13
to cuj...@googlegroups.com
So, I have:

var data = {
    user
: req.params.email,
    templates
: [
      __dirname
+ '/templates/signup_thankyou.html',
      __dirname
+ '/templates/signup_thankyou.txt',
      __dirname
+ '/templates/signup_internal.txt'
   
],
    bodies
: []
 
};

 
var tasks = [
    validateEmail
,
    readTemplates
,
    mergeTemplates
 
];

when_pipe
(tasks, data)
   
.then(...)



so, the tasks just keep passing, using and/or transforming the data object at every step in the pipeline.

For mergeTemplates, I have:

function mergeTemplates(data) {
 
return when.map(data.bodies, compileTemplate )
   
.then(
     
function(values) {
        data
.bodies = values; return data;
     
}
   
);
};

and the function compileTemplate "lifts" Underscore's template function:

var compileTemplate = when_fn.lift(
  _
.template
);



That Underscore.js template() function has the signature: _.template(templateString, [data], [settings])

How do I pass data.bodies and data itself (which contains the data to source for the template function)? Am I misusing when.map() or when.lift()? Does it somehow involve when.apply()?

Thanks.

Brian Cavalier

unread,
Jul 20, 2013, 9:52:59 PM7/20/13
to cuj...@googlegroups.com
Hi Paul,

It seems like you have the right idea here, passing the data object through.  The need to transform fields of the data object, which themselves might be promises, makes it a bit trickier, but still doable.  Here are a few thoughts:

You don't need to lift functions you pass to when.map.  when.map will manage the promises for you and ensure that it only passes fulfilled values to your mapping function.

One approach might be to simply assign the returned promise to data.bodies, and later use the version of all() in when/keys to wait for all the fields of data to fulfill.  Here's a modified version of part of your example that shows how you might do it:

var keys = require('when/keys');

function mergeTemplates(data) {
    data.bodies = when.map(data.bodies, _.template);
    return data;
}
//... modify data or other properties of data as needed
//.. and finally:
when_pipe(tasks, data)
    .then(keys.all)
    .then(function(data) {
        // data's fields are all fulfilled here
        // do stuff with data.bodies
    });

Hope that helps!

Paul Tiseo

unread,
Jul 20, 2013, 10:17:16 PM7/20/13
to cuj...@googlegroups.com
What if there's another step in the pipeline? I guess what I am trying to do is understand how to line up various types of tasks in the pipeline...


--
You received this message because you are subscribed to a topic in the Google Groups "cujojs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cujojs/7djHAvPY0Fs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cujojs+un...@googlegroups.com.
To post to this group, send email to cuj...@googlegroups.com.
Visit this group at http://groups.google.com/group/cujojs.
To view this discussion on the web visit https://groups.google.com/d/msgid/cujojs/49c5c922-4b47-4ffe-b68a-7db774ce4a75%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
____________________________________
Paul Tiseo
paulxtiseo@gmail.com

Brian Cavalier

unread,
Jul 22, 2013, 10:00:34 AM7/22/13
to cuj...@googlegroups.com
It really depends on the particular situation.  when/pipeline is intended to transform a value as it passes through the pipeline.  In this case, if you need to transform particular fields of that value, you'll either need to do what you did originally (forcibly waiting for the field to resolve), or use keys.all at specific points (or once at the end, depending on the situation), or write the code inside the tasks such that they can handle the case where a particular field might be a promise.

You could probably abstract that type of processing to make it reusable, i.e. a pipeline that transforms multiple fields of a data object in different ways.  When.js doesn't provide a high-level API for that out-of-the-box, but I'd be happy to help you work through it if you'd like.

Paul Tiseo

unread,
Jul 22, 2013, 9:02:48 PM7/22/13
to cuj...@googlegroups.com


On Saturday, July 20, 2013 9:52:59 PM UTC-4, Brian Cavalier wrote:
It seems like you have the right idea here, passing the data object through.  The need to transform fields of the data object, which themselves might be promises, makes it a bit trickier, but still doable.  Here are a few thoughts:

Good to know I'm on the right track. So, no harm if doing the when.then() inside the mergeTemplates() function? I wasn't sure if

return when.then()

returns a promise.

Brian Cavalier

unread,
Jul 22, 2013, 10:12:14 PM7/22/13
to cuj...@googlegroups.com
Do you mean the when.map(...).then(...)? Yep, that looks just fine.  Any promise's then() method always returns a promise, and since when.map() returns a promise, when.map().then() will indeed return a promise.
Reply all
Reply to author
Forward
0 new messages