javascript - Expected behavior for missing callbacks when providing a module API -
hopefully not abstract looking advice on expected behavior when creating node module api. finding in module implementation, checking make sure caller has provided callback before call getting messy. starting think user error not provide callback api needs 1 job.
being node beginner, general pattern modules? took @ node source code itself. seems in places use function swaps unsupplied callback generic 1 throws error. seems might lead confusion though.
thanks
you can provide default callback. depending of needs , importance of callback, can provide noop
function or function check if there error.
if (!callback) { callback = function noop() {} }
throwing or showing error you'll decide. (better not throw cases)
if (!callback) { callback = function handleerror(e) { if (e) console.log(e); // or if (e) throw(new error(e)) } }
Comments
Post a Comment