I have been writing NodeJS code for quite some time now, and seen different techniques implemented by people. My question is weather it is considered best practice having a function that as a signature returns a Promise
, throw directly when parameter assertion fails.
So you think we should do this? :
Throw sync way
function asyncPromise(param) {
assert(param, 'Missing required parameter');
// Param ok
return Promise.resolve(param);
}
OR this ? :
Rejected Promise
function rejectPromise(param) {
if (typeof param === 'undefined') {
return Promise.reject(new Error('Missing required parameter'));
}
// Param valid
return Promise.resolve(value);
}
My gut is telling me to go for the first way of directly throwing since wrong parameter input should be considered as something that should not continue, however when a function has a signature of returning a Promise, the caller expects a .catch handling of an error, and will miss the error if thrown directly.
What is your opinion?
via mitsos1os
No comments:
Post a Comment