I often see code like this:
(code completely made up from scratch)
something.someFunction('foobar')
.then(function(user) {
var email = user.email();
return user.getSomething();
})
.then(function(something) {
var metadata = something.metadata();
return user.getRelation(metadata);
})
.then(function(relation) {
return email.send(relation, metadata);
})
.catch(function(err) {
console.log(err);
})
;
Is this common practice? Personally, I think it's not very clean nor readable. What are alternatives? All I could come up with is something like put all needed stuff into an object and pass that around instead. However, that might get very large on long promises:
something.someFunction('foobar')
.then(function(user) {
return {
email: user.email(),
something: user.getSomething(),
};
})
.then(function(state) {
return {
email: state.email,
metadata: something.metadata(),
relation: user.getRelation(metadata),
// 'something' is not needed anymore ...
};
})
.then(function(state) {
return {
result: email.send(state.relation, state.metadata),
// and everything else that might be needed later on ...
};
})
.catch(function(err) {
console.log(err);
})
;
Would that even still work as expected? If not, what would be a way to create something similar? Again, I just came up with this code from scratch as a pure illustrating example.