My question is about promises. How can I use asn structure to do the following.
I try to delete a task. Tasks have documents.
For delete a task, first I get the task from from database, next get and delete task documents and finally delete the task.
All of this in a transaction of my database library. If something fails rollback transaction.
My initial idea is this code:
connection.beginTransaction(function (err) {
getTaskById(1)
.then(getTaskDocuments)
.then(deleteTaskDocuments)
.then(deleteTask)
.then(function(){
connection.commit(function (err) {
if (err) {
throw new Error()
}
res.json();
});
}).catch(
return connection.rollback(function() {
res.status(500).json()
})
});
It will be never works, because deleteTask, need the result of getTaskById, how can I solve this?
I will can modify promises or create a new promises. The only 2 facts that I can't change is that I need the database transaction and the order of delete, first documents and last task