I prefer to start my promise chains using a dummy value in order to catch any possible errors, see Starting promises. Let us see two delays one after another
1 | var Q = require('q'); |
When we shift more functions from the start of the promise chain to middle steps, we are forced to create temporary functions using partial application, compare
1 | delay(1000) |
I think the partial application using an external function, like R.partial is a little more clearer, because Function.prototype.bind has a dual purpose making it confusing.
1 | var R = require('ramda'); |
Taking it one (some might say unnecessary) step further, we can remove even more boilerplate. Instead of partially applying the function at the call time, we can create a curried function in the first place.
1 | var R = require('ramda'); |
Note, that I am using R.curryN to curry an unary _delay
as if it were a binary function. This
is necessary because we want to NOT execute the function as soon as a single argument is provided. Instead
we want to execute the _delay
function when it is called again (with the previous resolved value that will
be ignored).
1 | var delay = R.curryN(2, function _delay(N) { ... }); |
Just remember that if you need to use the curried version at the start of the promise chain you have to call it again
1 | delay(100)() |