Send ordered data from an array

How can I send ordered data from an array to Telegram Bot?

For example, I have text1, text2, text3in the array, but it sends them as text3, text1, text2, why not in the original order.

This is my code:

function sendAllText(msg, opts) {
   if (locale.keyboards[msg.text].text) {
      var i,j,tempstring;
      for (i=0,j=locale.keyboards[msg.text].text.length; i<j; i++) {
          tempstring = locale.keyboards[msg.text].text[i];
          bot.sendMessage(msg.chat.id, tempstring, opts);
      }
   }
}

The function is called as follows:

sendAllText(msg, opts);
+4
source share
2 answers

According to the documentation , it sendMessagereturns a promise: with this you can find out when the message was sent, and then send the next one, ... etc:

function sendAllText(msg, opts) {
   if (locale.keyboards[msg.text].text) {
      var i,j,tempstring, promise;
      promise = Promise.resolve();
      for (i=0,j=locale.keyboards[msg.text].text.length; i<j; i++) {
          tempstring = locale.keyboards[msg.text].text[i];
          promise = promise.then(bot.sendMessage.bind(bot,msg.chat.id, tempstring, opts));
      }
   }
}

.

+2

trincot, .

function sendAllText(msg, opts) {
  var textArr = locale.keyboards[msg.text].text;
  var promise = Promise.resolve();

  if (textArr.length) {
    textArr.forEach(function(value) {
      promise.then(function() {
        bot.sendMessage(msg.chat.id, value, opts);
      });
    });
  }
}
+1

Source: https://habr.com/ru/post/1675400/


All Articles