I have the following promise chain below and it appears to be quite messy (Each _create
* function returns a promise):
return new Promise(function (resolve, reject) {
_this.database.transaction(function (t) {
_this._createExternalAccount(payment, t)
.then(function (externalAccount) {
return _this._createExternalTransaction(externalAccount, payment, t)
.then(function (externalTransaction) {
return _this._createAddress(externalAccount, payment, t)
.then(function (address) {
return _this._createTransaction(address, payment, t)
.then(function (transaction) {
return _this._createGatewayTransaction(externalTransaction, transaction, payment, t)
.then(function (gatewayTransaction) {
t.commit();
resolve(bridgePayment);
});
});
});
});
})
.error(function (bridgePayment) {
t.rollback();
reject(bridgePayment);
});
});
I know there are Promise functions I can use like all()
and join()
but these seem to run the functions concurrently which I can't do because persisting to some tables require fields from the previously persisted tables. I'm hoping there is some way for me to do something like the following but I can't seem to find out how:
Promise.all(_this._createExternalAccount(payment, t), _this._createExternalTransaction(externalAccount, payment, t), _this._createAddress(externalAccount, payment, t))
.then(function(externalAccount, externalTransaction, address) {
// do logic
});