Currently I'm working with Node, Express and the Q library for promises. It works fine but I'm inserting each promise inside the following getting a horrible pyramid of doom.
I've checked I can avoid this with the following structure:
function validateInputFields(formName,req){
    var deferred = Q.defer()
      , connection  = req.conn || deferred.resolve(new Error('Internal error with connection.'));
    Services.data.splitBody(req.body)
    .then((arr) => {
        var inputFields = arr[0]
          , inputValues = arr[1];
        return Services.form.getFields(connection,formName)
    })
    .then((formKeys)            => { return Services.form.getMandatories(formKeys) })
    .then((dbMandatories)       => { return Services.form.allMandatoriesAreProvided(dbMandatories,inputFields) })
    .then((mandatoriesProvided) => { return Services.form.allFieldsBelongToform(formKeys,inputFields) })
    .then(()                    => {
        Services.form.correctFieldsType(inputFields,inputValues,formKeys)
        deferred.resolve();          
    })
    .catch((err) => next(err));
    return deferred.promise;
}
The problem here is that I can't access to some vars such as inputFields in the following promises and I was able to do when they where nested.
Other post suggest to use .spread in spite of .then but it doesn't work (even promises stop working).
Is there a smart way to solve this?
Thanks!
 
    