I have the below code where ‘Message B’ outputs before ‘Message A’.
Is this correct operation?  I thought that functions in onCreated got
executed first before helpers but that doesn’t seem to be the case. As a
result the variable x in the template, which is obtained from mongodb,
sometimes does and sometimes doesn’t have a value.
I also tried using Meteor.apply but that gave the same inconsistent results.
What am I doing wrong ?
Template.homepage.onCreated( function() {
    Meteor.call('getvalueofX', function(err,result){
        if(!err) {
              //Message A
             console.log("Message A: setting session variable x = " + result);
             Session.set("x",result);
        } 
    }
  });
Template.homepage.helpers({ 
    x: function() {
           // Message B
           console.log("Message B: getting session variable x = " + Session.get("x"));
           return Session.get("x");
    },
 });
<template name="homepage">
<script type="text/javascript">
 var x = '{{x}}';
  // the javascript variable is often undefined here 
  // which causes the app to break
</template>
I tried using Meteor.apply shown below instead but I get the same error:
// still doesn't work
 Meteor.apply('getvalueofX',[],true,function(err,result) { 
            if(!err) Session.set("x",result);
   } );