0

I have this switch case,

var res=null;
switch(case){
case "Delay":
        console.log("Start Delay");
        var timer = Meteor.setTimeout(function(){
            console.log("done Delay");
            res="sample";
        },15000);
        console.log("test Delay");
        break;
}
return res;

The code above will log "Start Delay" & "test Delay". Then it will start the timer. After 15000ms, it will log "done Delay". The problem here is the returning of res variable. Before the start of the timer, it already return res which is a null.

How can I return a variable after the timeout?

I also tried the suggested answer, this is my code for timeout and sleep function,

var timeout = function(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
var sleep = async function(fn, ...args) {
    await timeout(3000);
    return fn(...args);
}

I changed my switch case,

var res=null;
switch(case){
case "Delay":
        console.log("Start Delay");
        sleep(function(){
            console.log("done Delay");
            res="sample";
        },15000);
        console.log("test Delay");
        break;
}
return res;

But still the res variable returned null instead of "sample".

JMA
  • 974
  • 3
  • 13
  • 41
  • Possible duplicate of [What is the JavaScript version of sleep()?](http://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep) – Michel Floyd Apr 16 '17 at 17:49
  • Basically you want a synchronous sleep. See http://stackoverflow.com/a/39914235/2805154 – Michel Floyd Apr 16 '17 at 17:50
  • how can i use await sleep with switch case? – JMA Apr 16 '17 at 19:04
  • You would use `await sleep(15000);` in place of your `"Delay"` code above. – Michel Floyd Apr 16 '17 at 20:42
  • I added more explanation in my question. – JMA Apr 17 '17 at 21:05
  • You're supposed to call sleep with a number of ms as the only arg, not with an anonymous fn and then a delay value! Please re-read the example. – Michel Floyd Apr 17 '17 at 21:59
  • Is there an under lying reason as to why you are trying to delay code from running? As there may be better ways to achieve this without using Meteor.setTimeout – Calvin Apr 18 '17 at 10:25
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Yury Tarabanko Apr 18 '17 at 10:56

2 Answers2

0

Your switch statement is asynchronous and will not wait for your timeout to complete before returning your response.

You should make use of callback functions in this scenario. Here is a rough example of what you are possibly trying to achieve:

 var WorkWithResponse = function(response){
   console.log(response)
 }

var res=null;
var TestResponse = function(x, callback){

    switch(x){
    case "Delay":
            console.log("Start Delay");

            //This is the AngularJS $timeout service, 
            //replace with your own Meteor timeout method

            $timeout(function(){
              console.log('Delay over')
              res="Response Here";
              callback(res);
            },5000)
            console.log("test Delay");
            break;
    }
    return res;
}

TestResponse("Delay",WorkWithResponse)
Calvin
  • 61
  • 6
0

I used fibers/future package from npm. This package allow functions to wait to its async call before returning a value.

JMA
  • 974
  • 3
  • 13
  • 41