If anything, your pattern better guarantees the same user
than if client.setAsync()
was to return a user
. If the package was badly written or badly documented, you could receive back some other object entirely.
One proviso though - your user
object cannot be augmented by the intermediate client.setAsync()
process. There is no mechanism to do so unless user
itself was passed as a parameter to client.setAsync()
. It can only be assumed that that is not an issue in this case.
Working with what you have, the only obvious improvement would be to form your chain with one less .then()
as follows :
something.on('register', function(user) {
client.setAsync(config.id, user.id) // From a package (I can't set the return value)
.then(function() {
return handleNewUser(user);
})
.then(getSomeStuff)
.catch(function(err) {
console.error("Promise chain error: ", err);
});
});
You might enjoy reading through this question and its answers. In particular, find the answer entitled "Nesting (and) closures" and you'll see that accessing previous values in a closure is entirely reasonable and proper.