Q:

Can I untangle this nesting of 'when' promise invocations?

I'm new to the when.js javascript library, but I'm familiar with async programming in C#. That's why I find this code to be unwieldy:
filters.doFilter('filter1name', reqAndPosts).then(function(filter1) {
    filters.doFilter('filter2name', filter1).then(function(filter2) {
        filters.doFilter('filter3name', filter2).then(function (posts) {
            renderView(posts); 
        });
    });
    return filter1;
});

I basically want three methods to be called in sequence, with the output of each being piped to the next method. Is there anyway I can refactor this code to be more "sequence-like" - i.e. get rid of the nesting? I feel like there's something I'm missing with the when-framework here. I'm not doing it right, right?

A:

Since doFilter returns a promise, we can do
filters.doFilter('filter1name', reqAndPosts)
    .then(function(filter1) {
        return filters.doFilter('filter2name', filter1);
    })
    .then(function(filter2) {
        return filters.doFilter('filter3name', filter2);
    })
    .then(renderView);

A:

There is another option to have both advantages of cleaner indentation and previous results available: using withThis.
filters.doFilter('filter1name', reqAndPosts).withThis({}).then(function(filter1) {
    this.filter1 = filter1; // since we used withThis, you use `this` to store values
    return filters.doFilter('filter2name', filter1);
}).then(function(filter2) {
    // use "this.filter1" if you want
    return filters.doFilter('filter3name', filter2);
}).then(renderView);

