setTimeout ->
console.log 'foo'
setTimeout ->
console.log 'bar'
setTimeout ->
console.log 'baz'
, 1000
, 1000
, 1000
Is it possible to achieve the same result with jQuery.Deferred? Something like the following, perhaps:
someFunction()
.then(-> console.log 'foo')
.then(delay 1000)
.then(-> console.log 'bar')
.then(delay 1000)
.then(-> console.log 'baz')
Perhaps I'm wrong in thinking promises make it easy to write: Do A, then once that finishes, do B, then once that finishes, do C.
You can chain the
.then()
calls by returning a newDeferred
object. Specifically for delaying, you could use something like:DEMO: http://jsfiddle.net/yGcfu/
Yes it is:
(demo at jsfiddle.net)
Or, more common for a
delay
method, you omit the closure (one->
) and use