I recently started working with nodejs. I'd like to follow the async nature of it and write some helper functions which take one function as an argument and call that function only if certain criteria are met, for example:
function areMandatoryFieldsFilledOut( fields, httpRequest, httpResponse, callback ) {
/* some logic */
if( mandatory_fields_have_been_filled_out ) {
callback();
} else {
httpResponse.send('Mandatory fields have not been filled out!');
}
}
// usage
areMandatoryFieldsFilledOut(['username', 'password'], req, res, function () {
// this is executed only if both username and password values are supplied
}
What would be the best way write such functions? Is there a special word that You use for the beginning of the function name, like, 'ensure', e.g. 'ensureAuthenticated( callback )
?'
Is there a way to convince the reader of my code that the callback will be called only if the criteria are met just by function declaration (name and args)?