Is variable scope combined with promises good practice?
I often see code like this:
(the code is completely composed 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 find it not very clean and not readable. What are the alternatives? All I could think of was something like putting all the necessary things into an object and passing that instead. However, this can be very large with 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);
})
;
Will it still work as expected? If not, what is the way to create something like this? Again, I just came up with this code from scratch as a pure illustrative example.
+3
Num lock
source
to share
No one has answered this question yet
See similar questions:
598
or similar:
4086
2660
2038
1957
1850
1643
1513
1246
806
2