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

前端 未结 2 958
遥遥无期
遥遥无期 2021-01-08 00:28

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

相关标签:
2条回答
  • 2021-01-08 00:36

    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.

    0 讨论(0)
  • 2021-01-08 00:43

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

    0 讨论(0)
提交回复
热议问题