Search code examples
javascriptnode.jscoffeescripthubot

response.send understanding? (hubot-script)


I recently installed hubot and was testing it.

Now I saw sth. I don't understand:

Given this code (part of hubot-maps - maps.coffee file)

robot.respond /(?:(satellite|terrain|hybrid)[- ])?map( me)? (.+)/i, (msg) ->
mapType  = msg.match[1] or "roadmap"
location = encodeURIComponent(msg.match[3])
mapUrl   = "http://maps.google.com/maps/api/staticmap?markers=" +
            location +
            "&size=400x400&maptype=" +
            mapType +
            "&sensor=false" +
            "&format=png" # So campfire knows it's an image
url      = "http://maps.google.com/maps?q=" +
           location +
          "&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
          location +
          "&t=m&z=11"

msg.send mapUrl
msg.send url

Why I do get a response like this

enter image description here

where I get first url and then mapUrl?

I expect to get mapUrl first and then url


Solution

  • From this hubot PR, it looks like Hubot runs your msg.sends asynchronously, so there is no guaranteed order.

    As a side effect, listeners are now executed asynchronously. Behavior around message.done should remain the same (process until message.done is true).

    If you want mapUrl before url, you can take a look at the send function in the source code, which accepts a list of ordered strings.

    // Public: Posts a message back to the chat source
    //
    // strings - One or more strings to be posted. The order of these strings
    //           should be kept intact.
    //
    // Returns nothing.
    send (/* ...strings */) {
      const strings = [].slice.call(arguments)
      this.runWithMiddleware.apply(this, ['send', { plaintext: true }].concat(strings))
    }