S
S
sallyruthstruik2013-06-14 11:32:37
JavaScript
sallyruthstruik, 2013-06-14 11:32:37

Function Synchronization in JSDeferred

Started looking at the library. Why might this code not work?

Deferred = (require './libs/jsdeferred.js').Deferred

Deferred.define()

func = (s, t)->
  def = new Deferred()
  console.log "Im works"
  setTimeout(()->
    console.log s
  , t)
  return def
next(()->
  return func("Hello", 3000)
).next(()->
  return func("World", 1000)
)


In response I receive

$ coffee test.coffee 
Im works
Hello

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Zhurbitsky, 2013-06-14
@sallyruthstruik

You need to explicitly call deferred.call in the setTimeout callback
Try this

Deferred = (require './libs/jsdeferred.js').Deferred

Deferred.define()

func = (s, t)->
    def = new Deferred()
    console.log "Im works"
    setTimeout(()->
        console.log s
        def.call
    , t)
    return def
next(()->
    return func("Hello", 3000)
).next(()->
    return func("World", 1000)
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question