In AngularJS - mixing HTTP and custom promises with recursion I had posted a question about fixing a piece of code that involved returning promises and it was pointed out to me that I was unnecessarily creating wrapper promises. In following the suggestions, I landed up with a much cleaner code. I do have a followup question: In a situation like the below (made up example) where I ned to mix promise code with non-promise code, I don't see an option but to construct my own promise using $q.defer()
and returning d.promise
at the end. Is there a better/recommended way? And is it okay to mix my custom promise with functions that return their own promise?
function my_func(use_promise) {
var d = $q.defer();
if (!use_promise) {
x = do_a_sync_function_that_takes_time();
d.resolve(x)
return d.promise;
} else {
return do_a_promise_function_that_takes_time()
.then (function(data) {
return (data); // this gets promisified as we are in .then
})
.catch(function (err) {return "Error ";});
}
return d.promise;
}