Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async / Await not actually waiting in while loop

Towards then end of my project I realized that for its last component I'd have no choice but to use Async, Await, and Promise in order for the program to wait for an API call to finish and then continue. Although I've learned that there really is no "stopping" or "waiting" in Javascript. I've tried the code below and it works without the while-loop, but with it, it stops working. I want the program to send a tweet API call several times but I feel like the sendOff() function is not actually waiting, and the loop just hops over it because it's going too fast and not waiting for the API call to finish. Any help or a different method of attacking this would be very appreciated.

function sendTweets() {
  return new Promise ((resolve, reject) => {
    client.post('statuses/update', final_tweet, function(error, tweet, response) {
      if (error) {
        reject(error);
      }
      resolve(response);
    });
  });
}

async function sendOff() {
  await sendTweets();
}


while (1) {

  var final_tweet = {  // Create tweet struct
    status: "Hi!"
  }

  sendOff();

  sleep.sleep(4);
}

Edit: For those who ever have the same problem

The following code ended up working out

function sendTweets(final_tweet) {
  return new Promise ((resolve, reject) => {
    client.post('statuses/update', final_tweet, function(error, tweet, response) {
      if (error) {
        reject(error);
      }
      resolve(response);
    });
  });
}

(async () => {
  while (1) {
    var final_tweet = {  // Create tweet struct
      status: "Hi!"
    }
    await sendTweets(final_tweet);
    await sleep.sleep(4);
}
})().catch(e => { console.error(e) }) // Catch needed to prevent "Unhandled Promise Rejection" error

Huge thanks to @knobiDev @TARN4TION @Nitin Goyal

like image 509
Kyle Avatar asked Mar 20 '26 14:03

Kyle


2 Answers

You have to put while loop inside async and use await for statements in order to execute them synchronously.

you can wrap while inside async as shown below and omit unnecessary sendOff() method. Also you should pass final_tweet as a paramter.

function sendTweets(final_tweet) {
  return new Promise ((resolve, reject) => {
    client.post('statuses/update', final_tweet, function(error, tweet, response) {
      if (error) {
        reject(error);
      }
      resolve(response);
    });
  });
}

(async () => {
  while (1) {
    var final_tweet = {  // Create tweet struct
      status: "Hi!"
    }
    await sendTweets(final_tweet);
    await sleep.sleep(4); 
}
})()
like image 55
knobiDev Avatar answered Mar 23 '26 03:03

knobiDev


function sendTweets(final_tweet) {
  return new Promise ((resolve, reject) => {
    client.post('statuses/update', final_tweet, function(error, tweet, response) {
      if (error) {
        reject(error);
      }
      resolve(response);
    });
  });
}

async function sendOff(final_tweet ) {
  await sendTweets(final_tweet );
}


(async () => {
while (1) {

  var final_tweet = {  // Create tweet struct
    status: "Hi!"
  }

  await sendOff(final_tweet);

  sleep.sleep(4);
})()

like image 42
Nitin Goyal Avatar answered Mar 23 '26 02:03

Nitin Goyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!