Giter Site home page Giter Site logo

Comments (5)

dotcypress avatar dotcypress commented on May 6, 2024 6

Hey.

Yes, ES7's async/await will help you with a charm 😉

Otherwise, you might use any approaches to call Promise chain sequentially.

Your second example is absolutely correct, you might minimize code, like following:

bot.command('start', (ctx) => ctx.replyWithHTML("Hello")
  .then(() => ctx.replyWithHTML("This"))
  .then(() => ctx.replyWithHTML("Should"))
  .then(() => ctx.replyWithHTML("Send"))
  .then(() => ctx.replyWithHTML("Sequentially"))
  .then(() => ctx.replyWithHTML("This"))
)

PS: But this is not guarantee that messages arrive to user sequentially, it's really depends on Telegram servers logic.

from telegraf.

joshuaquek avatar joshuaquek commented on May 6, 2024

Dear dotcypress,

Thank you for extremely speedy reply!

Just to check, the syntax of

bot.command('start', (ctx) => ctx.replyWithHTML("Hello")
  .then(() => ctx.replyWithHTML("One"))
  .then(() => ctx.replyWithHTML("Two"))
)

be the same as

bot.command('start', (ctx) => ctx.replyWithHTML("Hello")
  .then(() => {
      return ctx.replyWithHTML("One")
  }).then(()=>{
      return ctx.replyWithHTML("Two")
  })
)

or would it be the same as

bot.command('start', (ctx) => ctx.replyWithHTML("Hello")
  .then(() => {
      ctx.replyWithHTML("One")
  }).then(()=>{
      ctx.replyWithHTML("Two")
  })
)

Does putting the return keyword make a difference?

Hope to hear from you soon!

from telegraf.

dotcypress avatar dotcypress commented on May 6, 2024

Hey 😃

We massively use Promises in Telegraf, and when you omit return keywork in last example - you just break Promise chain, and you will have similar result as in following example:

bot.command('start', (ctx) => {
  ctx.replyWithHTML("Hello")
  ctx.replyWithHTML("One")
  ctx.replyWithHTML("Two")
})

As you see, you lose all Promise benefits here. 😉

from telegraf.

dotcypress avatar dotcypress commented on May 6, 2024

@joshuaquek btw, good article about asynchronous js.

from telegraf.

joshuaquek avatar joshuaquek commented on May 6, 2024

Dear dotcypress,

ok, thank you!!! (:

Your answers have been super helpful!

from telegraf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.