0

So as the title says, I was wondering if this is possible.

const sleep =()=> new Promise((resolve, reject)=> setTimeout(resolve, 800));

const asyncMethod = async ()=> {
  await sleep();
  console.log("asyncMethod");
  return 1;
}

const getData =()=> {
    let data = null;
    asyncMethod().then(x=> data =x);
 ///   while(data ==null) This dose not work
    console.log("data", data)
}

getData() // the log should log asyncMethod and then data 1

Is this even possible?

Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
  • 3
    Nope, not really possible (there are technically ways around it, as with anything, but they're not practical at all). And even if it was practical to do sync requests, believe me, the pain of async exists for a good reason. Without it, you lock the browser and nobody can click on anything until (if) the request resolves. This Page Has Stopped Responding is not good UX. – ggorlen Jul 26 '22 at 04:04
  • Yah that is what I though. – Alen.Toma Jul 26 '22 at 05:18

0 Answers0