Could I use "Meteor._sleepForMs(1000)" on common folder?

I try to use mdg:validated-method and write it in common folder.
And then I get the method in the client, so I would like to use Meteor._sleepForMs() to wait for my testing (loading).
Please help me.

You could make a shim:

// Client
Meteor._sleepForMs = Meteor.setTimeout

Could clarify how to use it?

// Client
Meteor._sleepForMs = Meteor.setTimeout

// usage
Meteor._sleepForMs(1000); //???

Yep, that’s it. Just make sure it runs before Meteor._sleepForMs(1000); is called

The reason there is no Meteor._sleepForMs() on the client is because the server implementation uses Fibers, which aren’t available on the client.

The easiest thing to do on the client is to use Meteor.setTimeout() with a callback function that continues your test. Most testing frameworks let you do an Async test, and provide you with a done() callback, which you can call at the very end of your test. e.g.

Test.addAsync(function(test, done) {
  Meteor.setTimeout(function() {
    test.isTrue(checkIfSomethingHappened(), "no, it didn't");
  }, 1000);

In the long term, a good alternative is to use Promises (google it), especially because they’ve been officially adopted an in ES7 you can use them with async/await… but I’ll save that advanced topic for another time.

1 Like