It's being hard to learn how to avoid the deferred antipatern with the info I could find. I'm using Q library for Nodejs.
As it is possible to read, basically, we've to try to do not reject or answer to a promise with deferred.reject or deferred.resolve.
deferred.resolve must be replaced by a return sentence, so that point is quite simple.
The problem comes when I want use a promise inside a function which must reject the execution in some cases which aren't application errors. For example:
Services.data.isNotForbiddenFieldToUpdate(bodyKeys,forbidden)
.then(() => console.log('Finish'))
Where
isNotForbiddenFieldToUpdate: (fieldsToUpdate,forbiddenFields) => {
var deferred = Q.defer();
//DO A QUERY AND STORE IN result PARAM
if(err) deferred.reject(err)
else if(! result) deferred.reject(new Error('Query is empty.'));
else deferred,resolve();
return deferred.promise;
}
In this case, is not possible to return something different from deferred.promise. If I remove this and return true, the error is that then
is unknown.
How could I manage this kind of code without using deferred if is possible? Thanks.