Simulating delay on the server


#1

I’ve come up with a really goofy way of simulating delay on the server and I’d like to know if there’s a better way:

# This is some ass-backwards way to delay within a fiber on the server to simulate latency
if Meteor.isServer
  Future = Npm.require('fibers/future')

  delay = (ms, f) -> Meteor.setTimeout(f, ms)

  delayWithCallback = (ms, func, callback) ->
    delay ms, -> callback(null, func())

  syncify = (f) ->
    (args...) ->
      fut = new Future()
      callback = Meteor.bindEnvironment (error, result) ->
        if error
          fut.throw(error)
        else
          fut.return(result)
      f.apply(this, args.concat(callback))
      return fut.wait()

  syncDelay = syncify(delayWithCallback)

Meteor.methods
  newMsg: (text) -> syncDelay 100, => Messages.insert({text})

Meteor.publish 'messages', -> syncDelay 100, => Messages.find()

#2
Meteor._sleepForMs(2000);

methods are sync, so this is default for me :smiley:


#3

thanks. well at least I learned a thing or two about fibers, lol.