Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

377
Visualizações
Use `Promise.resolve` For Ordering Multi-function

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); 
});
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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)); 
over 4 years ago · Santiago Trujillo Relatório

0

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
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda