Created
April 29, 2015 15:22
-
-
Save calvintwr/cff31b4af91abe720ef1 to your computer and use it in GitHub Desktop.
Transaction with promises, ending with array of promises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return DB.sequelize.transaction(function (t) { | |
return DB.Something.create({ | |
// data to create | |
}).then(function(something) { | |
return [ | |
DB.SomethingElse.create({ | |
aValidatedField: 'aValueThatWould fail validation.' | |
}), | |
someInstance.save({fields: ['merchantCredits']}) | |
] | |
}); | |
}).catch(function(err) { | |
// error handling | |
}); | |
/* This will cause a Sequelize validation error to not bubble up to the outer catch, which subsequently doesn't cause a rollback. */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return DB.sequelize.transaction(function (t) { | |
return DB.Something.create({ | |
// data to create | |
}).then(function(something) { | |
return [ | |
DB.SomethingElse.create({ | |
aValidatedField: 'aValueThatWould fail validation.' | |
}), | |
someInstance.save({fields: ['merchantCredits']}) | |
] | |
}).spread(function(somethingElse, someInstance) { | |
return somethingElse; //just return something thenable makes it work. | |
}); | |
}).catch(function(err) { | |
// error handling | |
}); | |
/* This however, will work. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If anyone is looking at this gist, the solution is this:
return DB.sequelize.transaction(function (t) {