Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait example to docs. #8

Open
al6x opened this issue Feb 12, 2013 · 0 comments
Open

Add wait example to docs. #8

al6x opened this issue Feb 12, 2013 · 0 comments

Comments

@al6x
Copy link
Owner

al6x commented Feb 12, 2013

Useful in specs, to wait for condition to became true,

it "job should be finished" ->
  startSomeAsyncJob()
  # Expecting job to be finished at some point in future.
  wait -> expect(isJobFinished()).to.eql true

  # It's also possible to implement more elegant version with promises, but it's 
  # incompatible with Node.js async convention.
  expect(isJobFinished()).to.eventually.eql true
# Wait until function called without exception.
waitTimeout  = 3000
waitInterval = 5
wait = (arg) ->
  if _(arg).isFunction()
    func = arg
    startTime = moment().valueOf()
    while true
      try
        return func()
      catch err
        currentTime = moment().valueOf()
        if (currentTime - startTime) < waitTimeout
          setTimeout sync.defer(), waitInterval
          sync.await()
        else throw err
  else
    time = arg
    setTimeout sync.defer(), time
    sync.await()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant