0

Not sure if im just being dumb but is there any way other than possibly using some horrific while loop at the end of f() to convert f() into a sync function. I cant change someFun() i.e make it async function someFun() but i could add await to f() call if needed. So basically i want f() to return data that is obtained in the callback (imagine http call or something) I can do whatever i like in f() e.g. Promises etc but someFun can not change to be async function someFun()

<script>
  function f(){
    var data;
    var callback = function(){
      data = {name:"bob"};
      console.log("did some stuff");
    }
    setTimeout(callback, 100);
    
    return data;     
  }
  
  function someFunc(){
    console.log('start')
    var data = f();
    console.log('end', data)
  }
  someFunc();
</script> 
  • 1
    "using some horrific while loop at the end of f() to convert f() into a sync function" — What wouldn't work. The while loop would run forever and block the main event loop so the callback would never be called. – Quentin Jul 25 '22 at 14:09
  • 1
    "So basically i want f() to return data that is obtained in the callback" — Impossible. That data doesn't exist until the future. – Quentin Jul 25 '22 at 14:09
  • 1
    "can not change to be async function someFun()" — It **is** asynchronous already. The `async` keyword is just syntax to make it easier to manage promises. Making it return a promise is the standard way to achieve what you want. – Quentin Jul 25 '22 at 14:10
  • 1
    In your toy example, the solution is simple: don't use `setTimeout`. – Bergi Jul 25 '22 at 14:15
  • If you can't change the `signature` of `someFunc()` but you can change the *contents* of `someFunc()` then you can (1) modify `f()` to return a `Promise` and (2) append `.then()` to the result of `f()` to follow up on that `Promise`. – David Jul 25 '22 at 14:18
  • Fine. so some crappy while loop is out. OK. – Adrian Black Jul 26 '22 at 09:16
  • The point is i want to wait for the callback to return in the future. Thats what i want it do. When i call f() i want it to wait until the callback is returned and i get the data. – Adrian Black Jul 26 '22 at 09:18
  • If i make f() sync somehow - lets say i could do some crappy while loop - how would someFunc be async? I cant use async on f() definition as then i have to use await on the call then i have to define someFunc() as async and i cant change that. – Adrian Black Jul 26 '22 at 09:20
  • Thanks David. Yes but someFunc needs to synchronous. As what is returned from that is used by something. I didnt explain that clearly. My apologies. The issue is someFunc is sync currently and its return value is used by its caller (that i cant alter). someFunc return value is also dependent on the return from f() i.e. its a dependent sync chain currently. – Adrian Black Jul 26 '22 at 09:28
  • I think the answer is its not possible as currently written. And ultimately you would need to change the entire stack to add async and awaits to the functions involved in the call sequence. Or find another way or writing it. I didnt write the code. Im just trying to find a way to localise changes that would need to be made if possible to fix the current issue we have with the callback issue. – Adrian Black Jul 26 '22 at 09:32

0 Answers0