I have the following code in node v6.3.0 running on an api that runs two separate promises depending on a conditional of whether a param exists in the POST request.
if (paramExists) {
// call database with this query
User.filter(/* do something with param */)
.then(function (user) {
Data.filter(/* same in both conditions */)
.then(function (data) {
// join data and user
res.send(joinedData);
}) // omit catch for clarity
}) // omit catch for clarity
} else {
// call database with this query
User.filter(/* do something with header */)
.then(function (user) {
Data.filter(/* same in both conditions */)
.then(function (data) {
// join data and user
res.send(joinedData);
}) // omit catch for clarity
}) // omit catch for clarity
}
I am sure there is a way to DRY up this code so that the first promise in both conditions passes the user to the second promise, but I can't figure out how. Should I use a generator, or is there a way of doing this with promises that I am not getting?