I have some code like this:
function example(data){
asyncFnOne()
.all([asyncFnTwo(), data])
.spread(asyncFnThree)
.done();
};
It doesn't matter what those functions do. The problem I have is that I don't know how asyncFnThree can access both data from asyncFnTwo and from the function parameters. The way I solved the problem is not very readable. Is there a recommended way to do this in a clean way?
A second possible soultion would be
function example(data){
asyncFnOne()
.then(asyncFnTwo)
.then(function(result){
asyncFnThree(result, data);
})
.done();
};
};
But I think it is even less readable.
To use
Q.all
in the desired way you'd have to do the following:Or use a variable for the first part of the chain, so that you don't have to stuff everything in one expression and could use
return Q.all([promiseTwo, data])…
.The alternative way with the closure is totally fine as well, and rather standard. You might as well use any library that offers some kind of partial application, and use that instead of the function expression.