Is there any option in meteor to make execution delay for a while and stop execution untill current function is not executed

There is a method in Meteor
Meteor.call(function_name, parameters, callBack())

but suppose Meteor.call return some result and we following code

console.log(‘started’);
Meteor.Call(‘server_method’, params, callBack());
console.log(‘done’);

so here we see the result as like

started
done
Meteor.call result

but I want here to stop execution untill Meteor.call is completed.
I search and went though many post but didn’t find any solution

I am new in Meteor and needs my seniors help
Thanks in advance

This is the normal behaviour of an async call. You have to place any code that should run once the method has finished in the callback. This is due to the nature of async calls in the browser. JavaScript in the brower runs in a single thread, so calling async operations in a sync way would block the whole app. Only on the server you can use .call() in a sync fashion, thanks to Meteor’s “fibers”.

Thanks
I agree
but I want here functionality as following do …

function sleep(milliseconds) {
console.log(‘start’);
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
console.log(‘done’);
}

What exactly are you trying to do with that?!

1 Like

Strictly speaking, this should be your (original) code:

console.log('started');
Meteor.Call('server_method', params, (error, result) => {
  console.log('done');
});

Which is not to say that’s what you want - but as you wrote it, it will never return a result on the client. As @waldgeist says Meteor.call on the client MUST take the asynchronous form if you want to know the result, or if there was an error.

Having said that (and as @waldgeist also asked) what are you actually wanting to do? What’s the problem you’re trying to solve? You cannot always think in terms of synchronous (php, java, python …) code when writing Javascript.

since Meteor.call works as Async on client side
currently I want to get data from server and then want to validate it on client side before any further action

say …

var comapnyId = 23;
var info = {};
Meteor.call(‘getUserInfo’, comapnyId, function(err , res){
if(res){
info = res;
}
});

return info;

But here I get {} as return
because Meteor.call works Async
and it not wait and return info return empty value

So I want to ask is there any some way to return info after getting the value with Meteor.call

You cannot return the result of an async function like this. Instead, you should pass a callback method to your function and call this callback function inside the callback of Meteor.call().

Sample:

function getInfo(companyId, myCallback) {
  Meteor.call('getUserInfo', companyId, function(err, res) {
    if (res) {
       myCallback(res);
    }
  });
}

// inside the caller:
getInfo(companyId, function(info) {
   ... do whatever you like with the result ...
});

Or, a simpler variant would be:

function getInfo(companyId, myCallback) {
  Meteor.call('getUserInfo', companyId, myCallback);
}

// inside the caller:
getInfo(companyId, function(info) {
   ... do whatever you like with the result ...
});

This uses only one callback. This makes sense if you don’t want to do any additional checks on the result inside the getInfo() function.

var companyId = 23;
var info = {};
Meteor.call('getUserInfo', companyId, function(err , res) {
  if (!err) {
    info = res; // info will be set here (if the method works!)
    console.log(info);
 }
});
console.log(info); // info will not be set here - this code executes long before the method returns