skip to Main Content

I Have 3 Function and each one work with Promise.resolve Invidualy,
How Can use Promise.resolve For All?, When I Call All Functions, Those Aren’t Ordered

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));
      }
   }
}
function sendAllPhoto(msg, opts) {
   if (locale.keyboards[msg.text].photo) {
      var i,j,tempstring, promise;
      promise = Promise.resolve();
      for (i=0,j=locale.keyboards[msg.text].photo.length; i<j; i++) {
          tempstring = locale.keyboards[msg.text].photo[i];
          promise = promise.then(bot.sendPhoto.bind(bot,msg.chat.id, tempstring, opts));
      }
   }
}

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

When I call Functions, My Data is not Ordered, I’m Using Node telegram bot Api

bot.onText(//love/, function onLoveText(msg) {
  const opts = {
    reply_to_message_id: msg.message_id,
    reply_markup: JSON.stringify({
      keyboard: [
        ['Yes, you are the bot of my life ❤'],
        ['No, sorry there is another one...']
      ]
    })
  };
  sendAllText(msg, opts);
  sendAllPhoto(msg, opts);
  sendAllVideo(msg, opts); 
});

2

Answers


  1. You can use $q.all, The $q.all() method takes either an object or an array of promises and waits for all of them to resolve() or one of them to reject() and then executes the provided callback function. The values returned from the resolve function are provided depending on the way you give the promises to all().

    Example –

    var  promises = [sendAllText(), sendAllPhoto(), sendAllVideo()];
    
    $q.all(promises).then((values) => {
        console.log(values[0]); // value Text
        console.log(values[1]); // value Photo
        console.log(values[2]); // value Video
    });
    
    Login or Signup to reply.
  2. At the end of each of the three functions, right after their loops, add:

    return promise;
    

    Also make sure you define the promise variable at the start of the function, so it also is defined when the if condition is not true.

    For example, in the first function:

    function sendAllText(msg, opts) {
       var promise = Promise.resolve(); // <----
       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];
              promise = promise.then(bot.sendMessage.bind(bot,msg.chat.id, tempstring, opts));
          }
       }
       return promise; // <-----
    }
    

    Then in the last piece of code, chain your promises:

    sendAllText(msg, opts)
        .then(sendAllPhoto.bind(null, msg, opts))
        .then(sendAllVideo.bind(null, msg, opts)); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search