function rollBackTask(task) {
  var db = mongoose.connection.db;
  var chain = Promise.resolve();
  var lastIndex = task.steps.length - 1;
  var firstStep = task.steps[0];
  var lastStep = task.steps[lastIndex];
  var step;

  if (lastStep.state !== DONE && firstStep.state !== INITIAL) {
    for(var i = lastIndex; i >= 0 ; i--){
      step = task.steps[i];

      if (step.state === INITIAL || step.state === ROLLED) continue;

      //iife to avoid async issues
      (function(step){
        chain = chain.then(function() {
          return getRollbackFunc(step)(db, step, task);
        });
      })(step);
    }
  }

  return chain.then(function() {
    return task.remove();
  });
}
Rollback for a single task

@param task the task to roll back

@returns {Promise|*}