Skip to content

Instantly share code, notes, and snippets.

@rehia
Last active August 29, 2015 14:19
Show Gist options
  • Save rehia/b4eace8230baabf348fc to your computer and use it in GitHub Desktop.
Save rehia/b4eace8230baabf348fc to your computer and use it in GitHub Desktop.
Problem with promises : is it the right way ?
var client, payment;
return clientDao.get(id)
.then(function (foundClient) {
client = foundClient;
return paymentDao.get(paymentId);
}).then(function(foundPayment) {
client.credit(payment.amount);
payment.cancel();
return clientDao.save(client);
}).then(function() {
return paymentDao.save(payment);
}).then(function () {
client.flush();
payment.flush();
}, function (error) {
eventBus.publish('payment_cancel_error', {
error: error,
client: client || {id: id},
payment: payment || {id: paymentId}
});
});
var client;
return clientDao.get(id)
.then(function (foundClient) {
client = foundClient;
client.debit(amount);
return clientDao.save(client);
}).then(function () {
client.flush();
}, function (error) {
eventBus.publish('client_debited_error', {
error: error,
client: client || {id: id}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment