Javascript return value from setTimeout [duplicate]
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 3 years ago . I want do something like that: var x = function(){ if (controlVar === 0) { setTimeout(x, 300); } else { return value; }; Is there a method in js for call a function like in synchronous code (var z = x()) and return a value only when my controlVar turn in 1? This should not block, because when I use setTimeout the main loop shoul be able to take the control, but if