Skip to content

Overuse of join spread

Brian Cavalier edited this page Nov 5, 2014 · 2 revisions

tl;dr use when.try instead:

var result = when.try(threeArgumentFunction, promise1, promise2, promise3);

Using join + spread is a tempting pattern when you have 2 or more promises and want to call a function with their fulfillment values. For example:

var result = when.join(promise1, promise2, promise3).spread(threeArgumentFunction)

When you have a fixed/known number of promises, this join+spread dance obscures the real intent: calling threeArgumentFunction with the 3 fulfillment values. You probably wouldn't do the same thing in the non-promise case:

var result = threeArgumentFunction.apply(void 0, [value1, value2, value3]);

when you could just do the obvious and efficient thing:

var result = threeArgumentFunction(value1, value2, value3);

For promises, use when.try. It communicates the intent more clearly, achieves the same result, and is more efficient:

var result = when.try(threeArgumentFunction, promise1, promise2, promise3);

For more discussion, see: https://github.com/cujojs/when/issues/322

Clone this wiki locally