0

I am making a react website, and I need to wait for a request to pass before returning the value. I need to loop it so I can't stack .then(). Why does this code never return the value?

function PlaylistTracks(spotify, id, offset) {
  var r = null;
  spotify.getPlaylistTracks(id, { limit: 100, offset: offset }).then((response) => {
   r = response;
 });

 while (r == null) {
   console.log("Waiting for response");
 }
 return r;
  • 1
    Kind of. I make the function above async, and then it pushes the promise to the other sync function. I probably just have to make the main function async. – Brian Baldner May 25 '22 at 22:29
  • 1
    You cannot wait for a promise in a synchronous function: you can only return the promise and require the code calling your function to respond to its completion event. – StriplingWarrior May 25 '22 at 22:29
  • 2
    So yes, making the main function async is the right approach. – StriplingWarrior May 25 '22 at 22:30

0 Answers0