1

for example i got one variable which will be updated after finishing ajax request.

Is there another way to implement it without using async function? How to implement "wait" function in es5,es6?

let a = false;

setTimeout(()=>{
    a = true;
},10000);
  
(async function(){
  while(!a)
  {
    await new Promise(resolve=>{setTimeout(()=>{
      resolve('pause');
    },5)})  
  }
  console.log(a);
}(a))
  • What specifically are you trying to do? Right now you're in a tight loop spewing new timeouts as fast as they can be generated. In general you just stop doing things until your timeout ends and use a callback. – Dave Newton Oct 05 '19 at 15:21
  • Babel-polyfill dependence weight a lot – Иван Яковлев Oct 05 '19 at 15:23
  • Why would you want to do that? Javascript is single-threaded, so if you implement a sleep feature you’d be blocking every other script from running. – Lennholm Oct 05 '19 at 15:25
  • This seems to be an [XY problem](http://xyproblem.info/). Explain your higher level issue in more detail – charlietfl Oct 05 '19 at 15:32
  • @charlietfl in one class of my application exist one property. Some time the value of this property setted either asynchronously or not. Im calling this property in another class and i need to check if(property == null) wait(); – Иван Яковлев Oct 05 '19 at 15:39
  • Almost always the proper way to do async programming is to do it the way it's intended--don't busy-wait, use a callback or promise. – Dave Newton Oct 05 '19 at 16:00
  • You can write every `async function` as a normal function that uses promise chaining with good ol' `then()` calls. For a loop you will just need recursive calls. – Bergi Oct 05 '19 at 16:56

0 Answers0