Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

376
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda