When programming with promises in jQuery, I sometimes need to start out with a resolved promise, particularly when chaining .then()
in a loop like this as illustrated in Method #2 in this answer:
data.reduce(function(p, item) {
return p.then(function() {
return print(item);
});
}, $.Deferred().resolve().promise());
So, I know I can get a resolved promise with this:
$.Deferred().resolve().promise()
But, that seems kind of ugly and wasteful (three function calls). Is there a better (less code, fewer function calls) way to get a resolved promise in jQuery or a better way to write my .reduce()
loop above?