The Sailscasts Blog

← Back to blog

How to simulate latency in a Sails action

Kelvin Omereshone

I was going through my timeline on Twitter and came across this tweet by Mohamed Said on how he simulates latency in his local APIs.

Local frontends fetches data very quickly from local APIs. Which makes you miss the loading state. - Mohamed Said

Seeing the above tweet I recalled that Sails actually made provision for such a scenario and provides us with a property in our actions2 actions to do just that.

The simulateLatency property

Sails made it super easy to simulate latency in your actions2 actions. To do so pass in the number of milliseconds you want to simulate the latency for i.e how long you want the latency on that action to be, to a top-level simulateLatency property in your actions options.

// api/controllers/user/signin.js
module.exports = {
  friendlyName: 'Signin',
  description: 'Signs in a user',

  // Simulate latency for 1000 milliseconds
  simulateLatency: 1000

  inputs: {},

  exits: {},

  fn: async () {}
}

In the above example action, I omitted the rest of the action to focus on the simulateLatency property.

Conclusion

Alright! So now you have seen how not to miss loading state in your local frontend when making a request to a local Sails API. Happy deploying 🚀