syntax for Jquery.deferred , making synchronous function return promise
javascript, jquery-deferred, promise
Solution
To do something synchronously, but still use a promise, do:
function slowPromise() {
var def = $.Deferred();
// do something slow and synchronous
...
// resolve the deferred with the result of the slow process
def.resolve(res);
// and return the deferred
return def.promise();
}
The effect is that you still get a promise, but that promise is already resolved, so any `.then()` which is subsequently registered on it proceeds immediately.
The advantage of this pattern is that if you subsequently replace the synchronous code with something asynchronous the function still has the same external interface.
Problem
A quick question on how to use Jquery.deferred to make a slow synchronous function return a promise instead. What I've done so far is this : ``` function sayIt(ms) { setTimeout( function() { console.log('what I say'); }, ms); } function doIt() { return $.Deferred( function() { sayIt(2000); }).promise(); } doIt().then( function() { console.log('ah'); }); ``` the sayIt(2000) always goes through but the chained function after the 'then' never fires. If I do this : ``` doIt().then( console.log('ah')); ``` the 'ah' comes up right away, and then the 'what I say' 2000ms later - what I want is of course the opposite - that after two seconds I get 'what I say' and then 'ah' right after. Any suggestions appreciated!