The following is a typical promise function I'm dealing with.
var _delete = function(t, id) { return Promise.cast(Event.find({where: {id: id}}, {transaction: t})) .then(function(d){ if (d) { // ------- (*) return Promise.cast(d.updateAttributes({status: -1}, {transaction: t})) .then(function(){ // do inventory stuff return Promise.cast(Inventory.update({}).exec()) .then(function(d){ // do something }) }).then(function(){ // do product stuff return Promise.cast(Product.update({}).exec()) .then(function(d){ // do something }) }) } else { return Promise.reject('this transaction list does not exist'); } }); };
This looks fine until I come across a more complicated update / code generation that will become really messy.
I currently promise that 1. I have many useless return true statements, and my only goal is to move on to the next statement .then 2. the promise is programmed in a nested style. also input arguments are usually complex and have more than 1 argument, so i can't do something like this
.then(fun1).then(fun2)
... etc.
which makes it impossible to execute the .then operator 'tap' to enable / disable the function.
So my questions are: how do I do this right? Thanks..
the following really ugly things i'm talking about ....
var _process = function(t, tid) { var that = this; return Promise.cast(Usermain.find({where: {transaction_id: tid}})) .bind({}) // --- (*) .then(function(d){ this.tmain = d; return true; // ---- do nothing, just go to next thennable (is this correct) }).then(function(){ return Promise.cast(Userlist.findAndCountAll({where: {transaction_id: tid}})) }).then(function(d){ this.tlist = d; return true; // ---- do nothing, just go to next thennable (is this correct) }).then(function(){ if (this.tmain.is_processed) { return Promise.reject('something is wrong'); } if (this.tlist.count !== this.tmain.num_of_tran) { return Promise.reject('wrong'); } return Promise.resolve(JSON.parse(JSON.stringify(this.tlist.rows))) .map(function(d){ if (d.is_processed) return Promise.reject('something is wrong with tran list'); return true; // goto next then }); }).then(function(){ return Promise.cast(this.tmain.updateAttributes({is_processed: 1}, {transaction: t})); }).then(function(){ return Promise.resolve(this.tlist.rows) .map(function(d){ var tranlist = JSON.parse(JSON.stringify(d)); return Promise.cast(d.updateAttributes({is_processed: 1, date_processed: Date.now()}, {transaction: t})) .then(function(d){ if (!d) { return Promise.reject('cannot update tran main somehow'); } else { if (tranlist.amount < 0) { return Usermoney._payBalance(t, tranlist.user_id, -tranlist.amount); } else { return Usermoney._receiveBalance(t, tranlist.user_id, tranlist.amount); } } }); }); }); }
source share