When building a response back from the server using Angular to display private messages, I've come across code that seems to work fine but may not be following best practices for handling promises and errors. How should nested promises be handled in such cases to ensure best practices are being followed?
function listenMessageSend(data) {
var imData = {
message: data.data.id,
sender: currentUser().id,
reciever: $scope.reciever
}
Im.create(imData).then(function(im) {
Message.update(im.data.message, {im: im.data.id}).then(function(msg) {
User.get(msg.data.im.sender).then(function(sender) {
msg.data.im.sender = sender.data;
User.get(msg.data.im.reciever).then(function(reciever) {
msg.data.im.reciever = reciever.data;
});
});
if (msg.data.im.sender === currentUser().id || msg.data.im.reciever === currentUser().id) {
$scope.messages.push(msg.data);
}
})
});
};