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".