Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of Meteor.setTimeout() vs just setTimeout()?

Tags:

meteor

In Meteor, why would one use Meteor.setTimeout() over just normal setTimeout()?

What is the value of using Meteor.setTimeout() instead of just the vanilla setTimeout or setInterval?

like image 564
Mcope Avatar asked Jan 09 '15 00:01

Mcope


2 Answers

On the client, there's no difference between them.

On the server, when code is running for a specific user (for example in method calls), you need to use Meteor.setTimeout instead of window.setTimeout to make Meteor remember for which user the function should be called. In the time between the function passed to Meteor.setTimeout is called and when it's called, other users may have called methods on the server, chancing Meteor.userId to return their user id instead. Meteor.setTimeout will change back so Meteor.userId return the user id for the user the call to Meteor.setTimeout was made before calling the function passed to it.

It is a design decision.

like image 121
Peppe L-G Avatar answered Oct 21 '22 05:10

Peppe L-G


Using Meteor.setTimeout() ensures that this code is Fibers aware. Read more about Fibers: https://github.com/laverdet/node-fibers

like image 34
Mário Avatar answered Oct 21 '22 05:10

Mário