I want to do something like the following:
delay( 2500 )
.then( function () { console.log( Step 1 done ) } )
.then( delay( 7500 ) )
.then( function () { console.log( Step 2 done ) } );
So implementation of delay has been demonstrated many times before:
function delay( ms ) {
var deferred = Q.defer();
setTimeout( deferred.resolve, ms );
return deferred.promise;
}
But if I run the above in node.js I get:
... delay of 2500ms
Step 1 done
Step 2 done
... delay of ~7500ms
rather than what I expect to see:
... delay of 2500ms
Step 1 done
... delay of 7500ms
Step 2 done
In the examples provided on https://github.com/kriskowal/q/wiki/Examples-Gallery I can't find any examples of synchronous functions (functions that return a value without any callbacks involved) chained with promise functions.
Any ideas how to mix in synchronous actions with asynchronous promises?
I've tried:
function synchronousPromise() {
var deferred = Q.defer();
console.log( Synchronous function call );
deferred.resolve();
return deferred.promise;
}
delay( 2500 )
.then( function(){synchronousPromise()} )
.then( function(){delay( 7500 )} )
.then( function(){synchronousPromise()} );
And this outputs:
... delay of 2500ms
Time now is 2013-06-20
Time now is 2013-06-20
... delay of 7500ms
.. still not what I'm trying to achieve.