× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Simulate longer response from server in Meteor.js

Tomáš Hromník
Jun 10, 2015
<p>When I develop an app on localhost, everything is ok. Until I deploy it. I usually see longer responses from server and user interface seems to be frozen for a while. To simulate longer response on localhost you can use <em>Meteor._sleepForMs()</em>:</p> <pre>//server Meteor.methods({   'yourMethod': function() {     Meteor._sleepForMs(2000); //to simulate longer response sleep for 2 seconds     //do something         return 'something';   } });</pre> <p><strong>Don't forget to delete Meteor._sleepForMs() before deploy ;-). </strong></p> <p>Or if you often forget, you can check if the code runs on localhost:</p> <pre>//server Meteor.methods({   'yourMethod': function() {     if (/localhost/.test(Meteor.absoluteUrl())) {       Meteor._sleepForMs(2000); //to simulate longer response sleep for 2 seconds only on localhost     }     //do something         return 'something';   } });</pre> <p>You can use <a href="https://meteorhacks.com/introduction-to-latency-compensation.html">latency compensation</a> if you don't want to wait for the server response.</p> <p>Enjoy!</p>
comments powered by Disqus