synchronize
synchronize copied to clipboard
Add `wait` example to docs.
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()