How to render a template only after a Meteor.call is done?

I’ve got something like this:

Router.route('/:uid', {
  name: 'lookup',
  data: function () {
    Meteor.call('fetch', this.params.uid, function (error, result) {
      if (typeof error === 'undefined') {
        console.log(result);
        return result;
      } else {
        return {};
      }
    });
  }
});

The problem is, the lookup template is done rendering before the data is ready (the call to the fetch method makes a couple API calls), since Meteor.call is asynchronous. What I’d like is to use something like waitOn, but that doesn’t solve the async problem.

I’m out of ideas.

I wound up moving the Meteor.call within the template’s onRendered callback, and using Session.set to put the method’s result in a session var. Then in the route:

data: function () {
  return Session.get('info');
}
1 Like

In the future, you can us Promises to get around the async issue.

Check out https://github.com/chicagogrooves/deanius-meteor-promise

Not sure if this would solve the problem inside of the router’s data function (and your end solution is a better approach anyway) but it’s worth looking in to.