what you need is some sort of endpoint in server to revert, I am going to leave the logic of that implementation to you, just using stubs file.save()
for save and file.revert()
for reverting, and assuming that both return promises:
var files = [file1, file2, file3, ...], revertFiles =[];
var promise = Promise.resolve();
files.forEach( file =>
promise.then(() => file.save())
.then(() => revertFiles.unshift(file))
);
promise.catch(e => revertFiles.forEach( file => promise.then( () => file.revert() )));
basically, whenever you finish some action, make a stack of actions for reverting that, and when you face some failure, just do all the actions stacked up in the queue.
Edit: in es5 style
var files = [file1, file2, file3, ...], revertFiles =[];
var promise = Promise.resolve();
files.forEach(function(file){
promise.then(function(){
return file.save();
}).then(function(){
revertFiles.unshift(file);
});
});
promise.catch(function(e){
revertFiles.forEach(function(file){
promise.then(function(){
return file.revert();
});
});
});