Simulating delay on the server


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
      f.apply(this, args.concat(callback))
      return fut.wait()

  syncDelay = syncify(delayWithCallback)

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

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


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


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