by skylan_q · 13y ago · 0 comments
Is there some community-standard way of naming/declaring a JS function to let the programmer know just by reading the function's name that it will return a promise instead of a final computed value? I'm interested in knowing how people handle this. It's easy to forget that a function doesn't return a promise if the function name makes no mention of it, so I just make it explicit in the function name... but that's just how I handle it.
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.Otherwise, there's no "naming scheme" for promise-based functions.