Remember things if you are using Future in Meteor

Posted By : Parveen Kumar Yadav | 27-Dec-2016
If you are using Future to make your methods sync that you must be aware by some of the below aspects of Future. 1)When using future than must return something for example:-
 Meteor.methods({
 test: function(FKmarketAccObj, userObj) {
        var fut = new Future(),result;
        var requestBodyHeaderObj = Meteor.call('getRequestBodyAndHeaderForAPI) 
	if(requestBodyHeaderObj){
	result = Meteor.call('getSearchOrdersAPI', requestBodyHeaderObj, userObj);
	}
        return fut.wait();
    },
})
 
In the above method as there is use of future but there is not any return statement. So this will wait endlessly. Means the future is designed like this if it will not get ant fut['return]() statement than it will wait until it get the statement. But if you forgive to give the value it will wait endlessly.

2)Even your server is also block for that user until he refresh the browser. There is not any single hit comes into server if you miss the return statement. So you must add return statement like:-

 Meteor.methods({
 test: function(FKmarketAccObj, userObj) {
        var fut = new Future(),result;
        var requestBodyHeaderObj = Meteor.call('getRequestBodyAndHeaderForAPI) 
	if(requestBodyHeaderObj){
	result = Meteor.call('getSearchOrdersAPI', requestBodyHeaderObj, userObj);
	}
       	fut['return](result)  // this statement must be add
        return fut.wait();
    },
})
 

3)Never use return statement in loop like forEach or other because if you return future again and again for same than it will throw an error i.e:- Future resolved more than once. So to avoid this error always make a single return statement for future or always check for the length of array when it comes to last than only return.

In my personnel opinion avoid Future until its not required and use only once you study it in deapth Hope it will help Thanks!

About Author

Author Image
Parveen Kumar Yadav

Parveen is an experienced Java Developer working on Java, J2EE, Spring, Hibernate, Grails ,Node.js,Meteor,Blaze, Neo4j, MongoDB, Wowza Streaming Server,FFMPEG,Video transcoding,Amazon web services, AngularJs, javascript. He likes to learn new technologies

Request for Proposal

Name is required

Comment is required

Sending message..