Comment on Sane Async Patterns in JavaScriptparentComments−vec13yI'm getting in the habit of letting the caller decide, something like the following: myAsyncFunc = function (param1, param2, callback) { var usingPromises = (callback === undefined); var returnVal = usingPromises ? Q.defer() : this; someFunction(function (err, res) { if (usingPromises) { err ? returnVal.reject(err) : returnVal.resolve(res); } else { callback(err, res); } }); return returnVal; }; So if I call myAsyncFunc(foo, bar) it returns a promise and if I call myAsyncFunc(foo, bar, cb) it returns itself for chaining.
Comments
I'm getting in the habit of letting the caller decide, something like the following:
So if I call myAsyncFunc(foo, bar) it returns a promise and if I call myAsyncFunc(foo, bar, cb) it returns itself for chaining.